JeditL Posted August 11, 2009 Share Posted August 11, 2009 Hi I got an array like this: $array[$i][$j]=array('first'=>'one','second'=>'two','third'=>null); and I would like to add value to the field third later in a loop. I came up with this idea: $x=0; for($i=0;$i<$numrows;$i++) { for($j=0;$j<$numfields;$j++) { foreach($array[$i][$j] as $field) { $x++; if($x==3) { $row='three'; $x=0; } } } } But sure there must be easier way, right? The length of my code for the simple looking task feels just ridiculously long and complicated . I hope you can help to fix it. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
priti Posted August 11, 2009 Share Posted August 11, 2009 Can you try array_push() function in php ? Quote Link to comment Share on other sites More sharing options...
JeditL Posted August 11, 2009 Author Share Posted August 11, 2009 Can you try array_push() function in php ? Thanks for the reply. After googling for a while I found out how push_array() works and it didn't seem to be possible to add associative key to the field pushed but instead I found this solution: $array = array('animal'=>'dog', 'name'=>'Offenbach', 'owner'=>'Mr Smith'); $array = array_push_assoc($array, 'food', 'postmans leg'); function array_push_assoc($array, $key, $value){ $array[$key] = $value; return $array; } Link: http://www.phpro.org/examples/Array-Push-Assoc.html Eh.. Simple solution . Problem solved! Quote Link to comment 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.