1

I got 2 following arrays:

array(3) {
[0]=>
array(2) {
["id"]=> 5
["moreinfo"]=> 'moreinfo'
}
[1]=>
array(2) {
["id"]=>10,
["moreinfo"]=> 'moreinfo'

}
[2]=>
array(2) {
["id"]=>15
["moreinfo"]=> 'moreinfo'
} ... and so forth

and the second one:

array(3) {
[5] => "Mike"
[10]=> "Bob"
[15]=> "Alice" //here keys are user ids from the first array

I need to merge them in order to add the field name to the first array in order to get the following:

array(3) {
[0]=>
array(3) {
["id"]=> 5,
["name"]=> Mike,
["moreinfo"]=> 'moreinfo'
}
[1]=>
array(3) {
["id"]=>10,
["name"]=> Bob,
["moreinfo"]=> 'moreinfo'

}
[2]=>
array(3) {
["id"]=>15,
["name"]=> Alice,
["moreinfo"]=> 'moreinfo'
}

I tried to use array_merge but it did not produce the desired result. Also I used foreach but it also inserted only 1 name at the end of the array. Any ideas how to fix that? Thank you.

UPD My foreach try:

foreach($arr1 as $k => $v) {
            $arr1['name'] = $arr2[$v['id']];
        }
1
  • Show us what you tried with foreach and we can take it from there. Commented Oct 3, 2016 at 13:22

4 Answers 4

1

Assuming two arrays are $array1 and $array2 respectively, you need to add a new key value pair for 'name' in $array1 along with pre-existing records.

foreach ($array1 as &$arr) {
   /* $arr['id'] stores 5, 10, 15... So, that implies $array2[5] i.e Mike, $array2[10] i.e Bob and so on. */
   $arr['name'] = $array2[$arr['id']]; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

It updates only one item from the array. Also I tried with & but it did not help
0

It's very easy, go through each element of the first array and look for the name in second array by ID from first array.

So your code might look something like this,

foreach ($array as $key=>$element) {
   $element['name'] = $array2[$element['id']];
   $array[$key]=$element; //It's necessary to update the element.
}

In above case $array is a first array, and $array2 is the second array having names by id key.

2 Comments

This worked, but are there any ideas how to insert a 'name' key between 'id' and 'moreinfo' keys? Thank you
Order doesn't matter in case of the associative array. Don't worry about that so.
0
foreach ($array1 as $key=>$arr) {
    $array1[$key]['name'] = $array2[$arr['id']];
}

Comments

0

maybe this help you:

$arr1 = array(
  array("id"=>5,"moreinfo"=>"moreinfo"),
  array("id"=>10,"moreinfo"=>"moreinfo"),
  array("id"=>15,"moreinfo"=>"moreinfo")
);
$arr2 = array(5=>"mike",10=>"Bob",15=>"Alice");
//
$result = array_map(function($elm)use($arr2){
    $elm['name'] = $arr2[$elm["id"]];
    return array("id"=>$elm["id"],"name"=>$elm["name"],"moreinfo"=>$elm["moreinfo"]);
},$arr1);

var_dump($result);

result:

array (size=3)
  0 => 
    array (size=3)
      'id' => int 5
      'name' => string 'mike' (length=4)
      'moreinfo' => string 'moreinfo' (length=8)
  1 => 
    array (size=3)
      'id' => int 10
      'name' => string 'Bob' (length=3)
      'moreinfo' => string 'moreinfo' (length=8)
  2 => 
    array (size=3)
      'id' => int 15
      'name' => string 'Alice' (length=5)
      'moreinfo' => string 'moreinfo' (length=8)

1 Comment

Thank you for showing the variant how to save an order

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.