-1

How to check two arrays and search for matching keys and merge the values of the 1st array with the matching keys of the second array.Please help me as I'm new to this.

example :

1st array = {id => 11,name => 'name',age => 18 }


2nd array = {id,name,age,school}

I want to get the result by adding the matching values to the 2nd array

2nd array = {id => 11,name => 'name',age => 18,school => }
g.vihnga
  • 11
  • 5
  • i think this is answer you are searching for [stackoveflow ](http://stackoverflow.com/questions/13170230/php-combine-two-associative-arrays-into-one-array) – anuraj Apr 12 '16 at 04:51

3 Answers3

1

enter image description here

try this

$a = ['id' => 11,'name' => 'name','age' => 18];
$b = array_flip(['id','name','age','school']);
foreach($b as $key => &$value){
    $value = '';
}
$result = array_merge($b, $a);
Hanson
  • 99
  • 8
0

One of the simple way is looping

$first= array('id' => 11,'name' => 'name','age' => 18 );

$second = array('id','name','age','school');

foreach ($second as $value) {
    if(isset($first[$value])){
        $final[$value] =  $first[$value];
    }

};
print_r($final);

Second Array flip and array merge

$first = ['id' => 11,'name' => 'name','age' => 18];
$second= array_flip(['id','name','age','school']);
foreach($second as $key => s$value){
    $value = '';
}
$result = array_merge($second, $first);
print_r($result);
Ajay
  • 235
  • 2
  • 8
0

Use array_merge

<?php
$array1 = array('id' => '11', 'name' => 'name', 'age' => 18);
$array2 = array('id','name','age','school');

$array3 = array_merge(array_fill_keys($array2, null), $array1);

print_r($array3);
?>
J.K
  • 1,382
  • 1
  • 11
  • 27