【问题标题】:Problem with looping and array merging in PHPPHP中的循环和数组合并问题
【发布时间】:2011-10-31 22:01:06
【问题描述】:

我有两个二维数组。它们都包含id 和其他不那么相关的东西。如果id 匹配,我的工作就是将这两个数组合并在一起!

这是他们的样子:

array(3) {
  [0]=>
  array(3) {
    ["id"]=>
    string(3) "161"
    ["x"]=>
    string(1) "foo"
    ["y"]=>
    string(1) "bar"
    }
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(3) "164"
    ["x"]=>
    string(1) "foo"
    ["y"]=>
    string(1) "bar"
    }
  [2]=>
  array(3) {
    ["id"]=>
    string(3) "168"
    ["x"]=>
    string(1) "foo"
    ["y"]=>
    string(1) "bar"
    }
}

array(2) {
  [0]=>
  array(3) {
    ["id"]=>
    string(3) "161"
    ["z"]=>
    string(1) "baz"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(3) "164"
    ["z"]=>
    string(1) "baz"
}

结果应该是这样的:

array(3) {
  [0]=>
  array(3) {
    ["id"]=>
    string(3) "161"
    ["x"]=>
    string(1) "foo"
    ["y"]=>
    string(1) "bar"
    ["z"]=>
    string(1) "baz"
    }
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(3) "164"
    ["x"]=>
    string(1) "foo"
    ["y"]=>
    string(1) "bar"
    ["z"]=>
    string(1) "baz"
    }
  [2]=>
  array(3) {
    ["id"]=>
    string(3) "168"
    ["x"]=>
    string(1) "foo"
    ["y"]=>
    string(1) "bar"
    }
}

这就是我目前所拥有的。当然,它不起作用。

foreach ($rated_items as $item) {

    foreach ($posts as $post) {

        if ($post['id'] == $item['id']) {

            $posts = array_merge($posts, $item); // Doesn't work at all.

        }

    }

}

问题是我不知道如何将 current $post 合并到 current $item 然后将它们都添加到 $posts没有重复的数组。

感谢您的建议!

【问题讨论】:

    标签: php arrays loops merge foreach


    【解决方案1】:

    我不知道这是不是一个糟糕的语气,但我自己解决了我的问题。 :)

    $i = 0;
    foreach ($posts as $post) {
    
        $posts[$i] = $post;
    
        foreach ($rated_items as $item) {
    
            if ($post['id'] == $item['id']) {
    
                $posts[$i] += $item;
    
            }
    
        }
    
        ++$i;
    
    }
    

    编辑:

    更好的方法...

    foreach ($posts as $key => $post) {
    
        if (isset($rated_items[$key])) {
    
            $posts[$key] += $rated_items[$key];
    
        }
    
    }
    

    【讨论】:

    • 你不需要使用$i,你可以使用:foreach($posts as $key => &$post) 代替。也作为推荐使用参考(更有效),如:$posts[$i] =& $post;
    【解决方案2】:

    实现它的一种方法是,如果您首先重新索引您的数组,其方式如下所示:

    array(3) {
      [161]=>
      array(3) {
        ["id"]=>
        string(3) "161"
        ["x"]=>
        string(1) "foo"
        ["y"]=>
        string(1) "bar"
        }
      }
      [164]=>
      array(3) {
        ["id"]=>
        string(3) "164"
        ["x"]=>
        string(1) "foo"
        ["y"]=>
        string(1) "bar"
        }
      [168]=>
      array(3) {
        ["id"]=>
        string(3) "168"
        ["x"]=>
        string(1) "foo"
        ["y"]=>
        string(1) "bar"
        }
    }
    

    基本上,您将设置每个数组的键,即您的“id”值。 然后你可以毫无问题地对它们进行array_merge()。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      这应该可行:

      $newArray = array();
      foreach($array as $key => $arr){
          if(@$array2[$key]['id'] == $arr['id']){
              $newArray[] = array_merge($arr, $array2[$key]);
          } else {
              $newArray[] = $arr;
          }
      }
      

      【讨论】:

        【解决方案4】:
        $tmp = array_merge($posts , $rated_items);
        $final = array();
        foreach($tmp as $v){
            foreach($v as $k => $va){
                $final[$v['id']][$k] = $va; 
            }
        }
        

        $tmp = array_merge($posts , $rated_items);
        $final = array();
        foreach($tmp as $v){
            if($final[$v['id']]){
                $final[$v['id']] = array_merge($final[$v['id']], $v);
            }else{
                $final[$v['id']] = $v;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-04-12
          • 2011-05-18
          • 2021-10-04
          • 2011-07-01
          • 1970-01-01
          • 2015-10-30
          • 2012-07-18
          • 2012-12-20
          相关资源
          最近更新 更多