开发者

Problem with looping and array merging in PHP

I have two 2D arrays. They both contain id and other, not so-related, stuff. My job is to merge those two arrays together if id's match!

This is how they look:

array(3) {
  [0]=>
开发者_JAVA百科  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"
}

And this is how result should look:

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"
    }
}

And this is what I have so far. Of course, it doesn't work.

foreach ($rated_items as $item) {

    foreach ($posts as $post) {

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

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

        }

    }

}

The problem is that I don't know how to merge current $post to current $item and then, both of them, add to $posts array without getting duplicates.

Thanks in an advice!


I don't know is this a bad tone, but I resolved my problem myself. :)

$i = 0;
foreach ($posts as $post) {

    $posts[$i] = $post;

    foreach ($rated_items as $item) {

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

            $posts[$i] += $item;

        }

    }

    ++$i;

}

Edit:

Even better way...

foreach ($posts as $key => $post) {

    if (isset($rated_items[$key])) {

        $posts[$key] += $rated_items[$key];

    }

}


One way to achieve it is if you first re-index your arrays, in a way that will look like:

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"
    }
}

Basically you will set as key of each array, your "id" value. Then you can array_merge() them without a problem.

I hope it helps you.


this should work:

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


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

or

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜