1

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]=>
  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!

4 Answers 4

2

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];

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to use $i, you can use: foreach($posts as $key => &$post) instead. Also as recommendation use references (more efficient) like in: $posts[$i] =& $post;
1

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.

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.