Asked  7 Months ago    Answers:  5   Viewed   31 times

I know this is quite easily accomplished with a foreach, then a while->list, etc procedure, (I have already accomplished it), however I sense that my code is a bit dirty and it doesn't look like the best solution... I'm looking to use native PHP array functions to do the following:

I have two arrays that look like this:

[0] (Array)#2
  [rank] "579"
  [id] "1"
[1] (Array)#4
  [rank] "251"
  [id] "2"

[0] (Array)#2
  [size] "S"
  [status] "A"
  [id] "1"
[1] (Array)#15
  [size] "L"
  [status] "A"
  [id] "2"

And I need as a result something like the following:

[0] (Array)#2
  [size] "S"
  [status] "A"
  [id] "1"
  [rank] "579"

[1] (Array)#2
  [size] "L"
  [status] "A"
  [id] "2"
  [rank] "251"

Is there a way to be able to merge two arrays with the id value (or ay other) without going into a endless set of foreachs?

 Answers

40

Use array_merge_recursive()

$array = array_merge_recursive($array1, $array2);

or make your own function (it may be faster)

function my_array_merge(&$array1, &$array2) {
    $result = Array();
    foreach($array1 as $key => &$value) {
        $result[$key] = array_merge($value, $array2[$key]);
    }
    return $result;
}
$array = my_array_merge($array1, array2);
print_r($array);
Wednesday, March 31, 2021
 
Domiik
answered 7 Months ago
35

Simplest solution to make it completely dynamic apart from ID, do like below:

$arr2 = array_column($array2, "ID");

$finalArray = array();
foreach($array1 as $arr){
    $key = array_search($arr['ID'], $arr2);
    if($key ===false){
        $key = array_search(0, $arr2);
    }
    unset($array2[$key]['ID']);
    $finalArray[] =     array_merge($arr,$array2[$key]);
}

print_r($finalArray);

Output:- https://3v4l.org/1sDJs

Wednesday, March 31, 2021
 
John_BSDthos
answered 7 Months ago
66

Let assume first array be as $array1, second array be array2

$result = [];

foreach ($array1 as $key1 => $value1) {
    foreach ($array2 as $key2 => $value2) {
        if ($value1['Id']==$value2['Ref']) {
            $result[]=$value2+$value1;
        }
    }
}

The result will be as you expected

Saturday, May 29, 2021
 
Jesse
answered 5 Months ago
57

As to @splash58 comment:

You can use array-map. Here an example:

$array = [["5", "4"], ["BMW", "Ferrari"]];
$res = array_map(null, ...$array);

Now res will contain:

Array
(
    [0] => Array
        (
            [0] => 5
            [1] => BMW
        )
    [1] => Array
        (
            [0] => 4
            [1] => Ferrari
        )
)

If the array in 2 different var you can use:

$res= array_map(null, ["5", "4"], ["BMW", "Ferrari"]);
Saturday, July 31, 2021
 
Bitmap
answered 3 Months ago
28

This should work:

$result = array();
foreach($array as $elem) {
    $key = $elem['id'];
    if (isset($result[$key])) {
        $result[$key]['tval'] .= ',' . $elem['tval'];
    } else {
        $result[$key] = $elem;
    }
}

This basically groups elements by id, concatenating tvals (separated by ,).

Friday, August 6, 2021
 
EastSw
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :