enginemike Posted October 11, 2008 Share Posted October 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/128018-adding-information-ot-an-array/ Share on other sites More sharing options...
micmania1 Posted October 11, 2008 Share Posted October 11, 2008 $van_pool_members['member_3'] = array('first_name' => 'Tom', 'last_name' => 'Jones', 'member_number' => '3'); Quote Link to comment https://forums.phpfreaks.com/topic/128018-adding-information-ot-an-array/#findComment-662912 Share on other sites More sharing options...
Zane Posted October 11, 2008 Share Posted October 11, 2008 $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); Quote Link to comment https://forums.phpfreaks.com/topic/128018-adding-information-ot-an-array/#findComment-662921 Share on other sites More sharing options...
micmania1 Posted October 11, 2008 Share Posted October 11, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/128018-adding-information-ot-an-array/#findComment-662923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.