Jump to content

Recommended Posts

Hello

 

I am trying to add some information to an existing associative array.

 

My array is:

 

$van_pool_members =

array(

"member_1" => array('first_name' => "Joe, "last_name" => "Smith", "member_number" => "1"),

"member_2" => array('first_name' => "Mary, "last_name" => "Black", "member_number" => "2")

)

 

I am trying to figure out how to append a new member dynamically.  For example, I enter the information in a form and submit.

 

A new member is Tom Jones member number 3.

 

I have been researching to no avail and have yet to figure this out.

 

If anyone could offer some insight I would greatly appreciate it.

 

Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/128018-adding-information-ot-an-array/
Share on other sites

$van_pool_members['member_3'] = array('first_name' => 'Tom', 'last_name' => 'Jones', 'member_number' => '3');

 

Pretty much if you want it dynamic...it all depends on how your form works and being able to create the above code dynamically that's all there is to it.

if you have a POST form then
$van_pool_members['member_3']['first_name'] = $_POST['fname']
$van_pool_members['member_3']['last_name'] = $_POST['fname']
$van_pool_members['member_3']['member_number'] = count($van_pool_members);

And also, is there any need to have a member_number?

 

Maybe it would work better like this?

 

$van_pool_members['3'] = array('first_name' => 'Tom', 'last_name' => 'Jones');

 

foreach ($van_pool_members as $k => $v) {
echo 'Member #'.$k.'<br />
First Name: '.$van_pool_members[$k]['first_name'].'<br />
Last Name: '.$van_pool_members[$k]['last_name'];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.