1

假设我想组合 2 个数组,这些数组是名称 $year_into 和 $likes_dislikes。他们共享一个名为“名称”的密钥。我怎样才能做到这一点:

$year_info

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

还有这个 $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
        )
)

可以组合成一个数组 $complete,如下所示,如果“name”值匹配,则将来自第二个数组的信息添加到第一个数组中:

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
        )
)

我查看了已经提出的问题,但没有看到任何内容,也许我使用了错误的术语来描述问题。我被困在 foreach 循环中,因为如果我这样说

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'] )
}

我会为所有人的喜欢/不喜欢得到相同的值。最简单的方法是什么?

4

4 回答 4

2

这是一个疯狂的单线(我想它在技术上算作一条线),应该可以。

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

这仅适用于 PHP 5.3+,否则您可以这样做:

$complete = array();
foreach($year_info as $a){
    foreach($likes_dislikes as $ld){
        if($a['name'] === $ld['name']){
            $complete[] = $a + $ld;
            break;
        }
    }
}
于 2012-04-30T21:26:07.540 回答
1

超级懒惰,额外说明性的方法:

$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;
    }
  }
}

尽管这在大型数组中表现不佳,但如果可能,更改数据结构会更有意义。最好有简单地按名称键入的数据结构。你能控制你的输入吗?

于 2012-04-30T21:22:04.280 回答
1

只需遍历两个数组即可创建第三个数组。

$complete = array();
foreach ($year_info as $year) {
foreach ($like_dislikes as $like {
if ($year['name'] == $like['name']) {
$complete[] = array_merge($year, $like);
}
}
}
于 2012-04-30T21:23:10.957 回答
1

我会尝试做的是遍历两组数组(嵌套的 foreach 循环是一个不错的选择),检查两个数组中名称属性相同的实例。当它们存在时,您可以使用 array_merge() 将它们合并到一个新数组中。一个例子是:

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

假设您将数组命名为 $arr1 和 $arr2。

于 2012-04-30T21:24:23.000 回答