PHP 2-d associative array: to add a key-value pair to the 2nd dimension -
i have 2 arrays these:
$a = ( 0 => ( 'name'=> 'leonardo' ), 1 => ('name'=> 'matthew' ), ... ); $b = ( 40, 50, ...); and want produce third array:
$c = ( 0 => ('name' => 'leonardo', 'age' => 40), ('name' => 'matthew', 'age' => 50), ...); what's array function that?
you can use array_map. works -
$res = array_map(function($a,$b){$a['age'] = $b;return $a;}, $a,$b); var_dump($res); /** output **/ array 0 => array 'name' => string 'leonardo' (length=8) 'age' => int 40 1 => array 'name' => string 'matthew' (length=7) 'age' => int 50
Comments
Post a Comment