在PHP中组合数组

Lets say I want to combine 2 arrays and the arrays are names $year_into and $likes_dislikes. They share a key called "name". How can I make it so that this one:

$year_info

Array
(
    [0] => Array
        (
            [name] => JOE MONROE
            [year] => 1950
            [info] => his ghost still haunts us
        )
    [1] => Array
        (
            [name] => FUTUREMAN
            [year] => 1930
            [info] => RIP
        )
)

and this one $likes_dislikes

Array
(
    [0] => Array
        (
            [name] => JOE MONROE
            [likes] => cornbread
            [dislikes] => pain
        )
    [1] => Array
        (
            [name] => E. Case
            [likes] => chaos
            [dislikes] => order
        )
    [2] => Array
        (
            [name] => FUTUREMAN
            [likes] => mustard
            [dislikes] => mayo
        )
)

Can be combined into one array $complete that looks like this, where the information from the 2nd array is added to the 1st if the "name" value matches:

Array
(
    [0] => Array
        (
            [name] => JOE MONROE
            [year] => 1950
            [info] => his ghost still haunts us
            [likes] => cornbread
            [dislikes] => pain
        )
    [1] => Array
        (
            [name] => FUTUREMAN
            [year] => 1930
            [info] => RIP
            [likes] => mustard
            [dislikes] => mayo
        )
)

I've looked through the already asked questions but don't see anything, maybe I'm using the wrong terminology to describe the problem. I'm stuck on the foreach loop because if I say like this

foreach ($year_info as $y){
    $complete[]=array('name'=>$y['name'], 'year'=>$y['year'], 'info'=>$y['info'], 'likes'=$likes_dislikes[0]['likes'],'dislikes'=>$likes_dislikes[0]['dislikes'] )
}

I'll just get the same values for likes/dislikes for all. What is the simplest way to do this?

What I would try to do is loop through both sets of arrays (a nested foreach loop would be a good choice), checking for instances where The name attribute is the same in both arrays. When they are, you can use array_merge() to merge them into a new array. An example would be:

$newArray = array();
foreach ($arr1 as $first) {
    foreach ($arr2 as $second) {
        if($first["name"] == $second["name"]){
            array_push($newArray, array_merge($first, $second));
        }
    }
}

Assuming you named your arrays $arr1 and $arr2.

The super-lazy, extra-expository approach:

$complete = array();

foreach($year_info as $yr){
  $name = $yr['name'];
  foreach($likes_dislikes as $ld){
    if($ld['name']!=$name){
      continue;
    }else{
      $new_entity = array();
      $new_entity['name'] = $name;
      $new_entity['year'] = $yr['year'];
      $new_entity['info'] = $yr['info'];
      $new_entity['likes'] = $ld['likes'];
      $new_entity['dislikes'] = $ld['dislikes'];
      $complete[] = $new_entity;
      break;
    }
  }
}

Though this will perform poorly with large arrays, it would make more sense to change the data structure if possible. It would be better to have data structures that were simply keyed by name. Do you have control over your input?

just loop over both arrays to create a third one.

$complete = array();
foreach ($year_info as $year) {
foreach ($like_dislikes as $like {
if ($year['name'] == $like['name']) {
$complete[] = array_merge($year, $like);
}
}
}

Here's a crazy one-liner (I guess it technically counts as one line), that should work.

$complete = array_map(function($a) use($likes_dislikes){
    foreach($likes_dislikes as $ld){
        if($a['name'] === $ld['name']){
            return $a + $ld;
            break;
        }
    }
}, $year_info);

This will only work in PHP 5.3+, otherwise you can do it like this:

$complete = array();
foreach($year_info as $a){
    foreach($likes_dislikes as $ld){
        if($a['name'] === $ld['name']){
            $complete[] = $a + $ld;
            break;
        }
    }
}