Friday, 15 March 2013

foreach - PHP add key value to array with each loop itteration -



foreach - PHP add key value to array with each loop itteration -

i have array want create new array key value pairs. think know what's required, need help syntax.

foreach ($stuff $thing){ $thing1 = $thing->prop1; $thing2 = $thing->prop2; // need set $newstuff = array($thing1 => $thing2); $newstuff[] = ?? }

thanks helpful advice!

can utilize array_map() instead of foreach(). example:

$newstuff = array_map(function($v){return array($v->prop1=>$v->prop2);}, $stuff);

and using foreach():

foreach ($stuff $thing){ $newstuff[] = array($thing->prop1=>$thing->prop2); }

php foreach

No comments:

Post a Comment