【问题标题】:Combine two arrays in PHP [closed]在 PHP 中合并两个数组 [关闭]
【发布时间】:2014-05-06 10:31:38
【问题描述】:

这是我拥有的多维数组(第一个数组)

 Array( [1] => Array
        (
            [ID] => 56
            [heading] => 'w'
            [content] => Abcdefghijk 
        )
      .................

)

和第二个数组

Array
(
    [w] => 2
    [e] => 2
    [d] => 2
    ..

)

在输出中,只有当标题的值作为 index 存在于第二个数组中时,我才想将第一个数组与第二个数组合并。 该索引的值也应该存储在 index -score 下。

Array( [1] => Array
        (
            [ID] => 56
            [heading] => 'w'
            [content] => Abcdefghijk 
            [score]=>2
        )
     ......

 )

【问题讨论】:

  • 你想要哪个2
  • 你想如何获得分数?你想用第二个数组做什么......只需取第一个或第二个或第三个值......或者什么......
  • score 是从哪里来的?
  • 猜猜答案是对的 :)
  • $score = $scores[$array[1]['heading']]; $array[1]['score'] = $score;Demo.

标签: php arrays


【解决方案1】:

我不确定我是否理解这个问题,但我假设您希望将heading 设为score。那好吧。

foreach ($frist as $key => $value) {
    $frist[$key]['score'] = $second[$value['heading']];
}

【讨论】:

  • 因为我读了这篇文章.. 这很有意义:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多