1

UPDATE: One array is an actual array, the other is an object array.

I'm wanting to combine two arrays repeating data where keys match.

$arr1 = array(
  object(stdClass){"id" => 1, "key" => 1, "content" => "blah", "extra" => "extra data"},
  object(stdClass){"id" => 2, "key" => 2, "content" => "bleep", "extra" => "extra data"},
  object(stdClass){"id" => 3, "key" => 3, "content" => "bloop", "extra" => "extra data"},
  object(stdClass){"id" => 4, "key" => 2, "content" => "zip", "extra" => "extra data"},
  object(stdClass){"id" => 5, "key" => 1, "content" => "zorp", "extra" => "extra data"}
);

$arr2 = array(
  array("id" => 1, "otherkey" => 1, "title" => "foo"),
  array("id" => 2, "otherkey" => 2, "title" => "bar"),
  array("id" => 3, "otherkey" => 3, "title" => "baz")
);

$desiredArr = {
  array("id" => 1, "key" => 1, "content" => "blah", "extra" => "extra data", "title" => "foo"),
  array("id" => 2, "key" => 2, "content" => "bleep", "extra" => "extra data", "title" => "bar"),
  array("id" => 3, "key" => 3, "content" => "bloop", "extra" => "extra data", "title" => "baz"),
  array("id" => 4, "key" => 2, "content" => "zip", "extra" => "extra data", "title" => "bar"),
  array("id" => 5, "key" => 1, "content" => "zorp", "extra" => "extra data", "title" => "foo")
};

I've tried array_merge(), array_walk_recursive(), array_combine(), array_push() with nested foreach() and am still working on a winning combination of such but the closest I've gotten is appending $arr2 to $arr1 which results in:

$newArr = {
  array("id" => 1, "key" => 1, "content" => "blah", "extra" => "extra data"),
  array("id" => 2, "key" => 2, "content" => "bleep", "extra" => "extra data"),
  array("id" => 3, "key" => 3, "content" => "bloop", "extra" => "extra data"),
  array("id" => 4, "key" => 2, "content" => "zip", "extra" => "extra data"),
  array("id" => 5, "key" => 1, "content" => "zorp", "extra" => "extra data"),
  array("id" => 6, "otherkey" => 1, "title" => "foo"),
  array("id" => 7, "otherkey" => 2, "title" => "bar"),
  array("id" => 8, "otherkey" => 3, "title" => "baz")
};

If someone has run into this before your guidance is much appreciated!

mezzomix
  • 305
  • 2
  • 12

1 Answers1

1

You can use map to reiterate the $arr1

Use array_search and array_column to search of the key of $arr2

$arr1 = array(
  (object)array("id" => 1, "key" => 1, "content" => "blah", "extra" => "extra data"),
  (object)array("id" => 2, "key" => 2, "content" => "bleep", "extra" => "extra data"),
  (object)array("id" => 3, "key" => 3, "content" => "bloop", "extra" => "extra data"),
  (object)array("id" => 4, "key" => 2, "content" => "zip", "extra" => "extra data"),
  (object)array("id" => 5, "key" => 1, "content" => "zorp", "extra" => "extra data")
);

$arr2 = array(
  array("id" => 1, "otherkey" => 1, "title" => "foo"),
  array("id" => 2, "otherkey" => 2, "title" => "bar"),
  array("id" => 3, "otherkey" => 3, "title" => "baz")
);

$desiredArr = array_map(function($v) use($arr2) {
    $v = (array)$v;
    $key = array_search($v["id"], array_column($arr2, 'id'));
    $v["title"] = $arr2[$key]["title"];
    return $v;
}, $arr1);

echo "<pre>";
print_r( $desiredArr );
echo "</pre>";

This will result to:

Array
(
    [0] => Array
        (
            [id] => 1
            [key] => 1
            [content] => blah
            [extra] => extra data
            [title] => foo
        )

    [1] => Array
        (
            [id] => 2
            [key] => 2
            [content] => bleep
            [extra] => extra data
            [title] => bar
        )

    [2] => Array
        (
            [id] => 3
            [key] => 3
            [content] => bloop
            [extra] => extra data
            [title] => baz
        )

    [3] => Array
        (
            [id] => 4
            [key] => 2
            [content] => zip
            [extra] => extra data
            [title] => foo
        )

    [4] => Array
        (
            [id] => 5
            [key] => 1
            [content] => zorp
            [extra] => extra data
            [title] => foo
        )

)
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • I've updated the question correcting the array and exchanging id for keys to make it more legible. I don't think this interferes with your answer but I am getting back a 500 when I try it. Working through it now. – mezzomix Mar 29 '18 at 17:14
  • @bardicwarrior The updated is still not a valid array. – Eddie Mar 29 '18 at 17:19
  • @bardicwarrior But I think I know what you mean. Please check my updated answer. – Eddie Mar 29 '18 at 17:26
  • 1
    my apologies - I thought it was easier to see the issue when all the arrays lined up in that minimal format - and I thank you for continuing! This worked for me, I haven't worked with array_column before nor have I used array_search - thank you for introducing me to these tools! – mezzomix Mar 29 '18 at 17:35
  • @bardicwarrior Happy to help :) – Eddie Mar 29 '18 at 17:36