Merge two indexed arrays of indexed arrays based on first column value
Asked Answered
S

2

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']
]
Socman answered 16/1, 2012 at 12:4 Comment(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?Auberbach
what's wrong with loops?Slut
@ 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.Socman
H
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);
?>
Hodeida answered 16/1, 2012 at 12:8 Comment(3)
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/RaEORPostlude
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.Postlude
Why is this answer accepted? The sample input does not resemble the indexed array of indexed arrays, and the output is not the expected oneSiegler
P
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);
Postlude answered 17/1, 2021 at 8:17 Comment(2)
The result of this code doesn't have the same keys as the inputsMccord
"If you wish to reindex the result, just call array_values() on it."Postlude

© 2022 - 2024 — McMap. All rights reserved.