Merging two arrays using keys
So I have two arrays.
$user_ids = array('123','124','125');
$names = array('john','bob','susie');
All arrays are now mapped. This means 123 is the user_id for john, 124 is the user_id for Bob, etc. (So ββboth arrays have matching keys)
But I want to get a multidimensional array for each user with their user_id and name instead of having them separately.
+3
source to share
2 answers
You can try using array_combine()
or as array_map()
per your requirement
$user_ids = array('123','124','125');
$names = array('john','bob','susie');
$new_array = array_combine($user_ids, $names);
or
$new_array = array_map(function($name, $id){
return array('id'=>$id, 'name'=>$name);}, $names, $user_ids
);
+2
source to share