2

I have two arrays like this:

$array1 = [
    [10, 'Some Name..'],
    [11, 'Some Name..'],
    [13, 'Some Name..'],
];

$array2 = [
    [13, 'Viewed']
];

How can I merge these two arrays without looping? Is there any php functionality available for this? I need this kind of output:

[
    [10, 'Some Name..'],
    [11, 'Some Name..'],
    [13, 'Some Name..', 'Viewed']
]
3
  • Why are you resrticted to solutions that don't use looping? Are you aware that any solution will involve a loop, even if it doesn't appear explicitly in the PHP code? Commented Jan 16, 2012 at 12:08
  • what's wrong with loops? Commented Jan 16, 2012 at 12:08
  • @ Zulkhaery Basrul - Yes I checked that question. Please read my question first. I can do that by looping my arrays in my php code. But I want to know whether I can do it with out using any loops. One more think array_merge will not give proper result what I printed in my question. Commented Jan 16, 2012 at 12:53

2 Answers 2

5

You can use the PHP function array_merge_recursive. See the example:

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Why is this answer accepted? The sample input does not resemble the indexed array of indexed arrays. Too many researchers have been fooled by this answer! Proof of failure and reason for my DV: 3v4l.org/RaEOR
Please do not blindly copy-paste text directly from the manual. Answers are expected to be tailored to the OP's question and honor the sample data when provided.
Why is this answer accepted? The sample input does not resemble the indexed array of indexed arrays, and the output is not the expected one
2

There will not be a non-loop way to accomplish this. Maybe you are seeking function-based syntax, but for this scenario I think this will only introduce unnecessary convolution.

For best efficiency, generate a result array that doubles as a lookup array. If the key already exists in the result array, you only need to push the [1] value into that row. If you wish to reindex the result, just call array_values() on it.

Code: (Demo)

$array1 = [
    [10, 'Some Name..'],
    [11, 'Some Name..'],
    [13, 'Some Name..'],
];

$array2 = [
    [13, 'Viewed']
];

$result = [];
foreach (array_merge($array1, $array2) as $row) {
    if (!isset($result[$row[0]])) {
        $result[$row[0]] = $row;
    } else {
        $result[$row[0]][] = $row[1];
    }
}

var_export($result);

Functional style: (Demo)

var_export(
    array_reduce(
        array_merge($array1, $array2),
        function($result, $row) {
            if (!isset($result[$row[0]])) {
                $result[$row[0]] = $row;
            } else {
                $result[$row[0]][] = $row[1];
            }
            return $result;
        }
    )
);

Because this task needs to merge on the first column and retain (append) all second column values, a conditionless approach can be used for the same result. Code: (Demo)

$result = [];
foreach (array_merge($array1, $array2) as [$id, $value]) {
    $result[$id][0] = $id;
    $result[$id][] = $value;
}

var_export($result);

2 Comments

The result of this code doesn't have the same keys as the inputs
"If you wish to reindex the result, just call array_values() on it."

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.