7

I have tried to merge two different arrays into a single array. Can any one help me please?

i have array like this

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

[0] (Array)#2
  [size] "S"
  [rank] "251"
[1] (Array)#15
  [size] "L"
  [rank] "579"

i need like this

[0] (Array)#2
  [size] "S"
  [rank] "251"
  [id] "1"
[1] (Array)#15
  [size] "L"
  [rank] "579"
  [id] "1"
Chris
  • 2,955
  • 1
  • 30
  • 43
user3686600
  • 121
  • 6

5 Answers5

4

Untested, but this should work, or at least get you close.

for ($array1 as $key1 => $value1) {
    for ($array2 as $key2 => $value2) {
        if ($value1['rank'] == $value2['rank']) {
            $result[$key1] = [$value2['size'], $value1['rank'], $value1['id']];
        };
    };
};
glend
  • 1,592
  • 1
  • 17
  • 36
  • This will change one of the arrays ($array1). you have to make sure that's accepted based on your criteria. usually a merge process should leave both original roots intact and create a new _(third)_ entity as the result – Ali Jul 02 '15 at 09:07
  • 1
    Fixed, we/i could make it figure out the keys it needs to copy. Don't see any reason to add that as of yet. – glend Jul 02 '15 at 09:13
1
foreach($arr1 as $key1 => $data1){
    foreach($arr2 as $key2 => $data2){
        if($data1['rank']==$data2['rank']){
            $result[] = array_merge($data1, $data2);
        }
    }
}
print_r($result);
Ali
  • 2,993
  • 3
  • 19
  • 42
Ambal Mani
  • 295
  • 1
  • 5
  • 16
  • a proper merge solution but you are assuming the **rank** key is always present and unique in both arrays (which is fine as far as thats the case :) ) – Ali Jul 02 '15 at 09:09
  • 1
    if you have any unique key mens you used that one otherwise you don't need to check if condion and seconf for loop. :) – Ambal Mani Jul 02 '15 at 09:19
  • Agreed, as far our assumption around the **rank** is valid ;) – Ali Jul 02 '15 at 09:27
0

While I do not understand why you want to merge the arrays this way, here's a way to merge the arrays by their sequences. so the first child of array1 will be merged with the first child of array2 etc.

<?php

$array1 = [
    [
        'rank' => 579,
        'id' => 1
    ],
    [
        'rank' => 251,
        'id' => 2
    ]
];

$array2 = [
    [
        'size' => 'S',
        'rank' => 251
    ],
    [
        'size' => 'L',
        'rank' => 579
    ]
];


foreach ($array1 as $key => &$data) {
    if (isset($array2[$key])) {
        $data = array_merge($data, $array2[$key]);    
    }
}

var_dump($array1);
jbe
  • 1,722
  • 1
  • 12
  • 20
0
//save keys of ranks in the 1st array
$keys = array(); 
foreach($arr1 as $k => $v)
   $keys[$v['rank']] = $k;

$res = array();
// Walk through the 2nd array and make result
foreach($arr2 as $k => $v) 
 if (isset($keys[$v['rank']])) 
    $res[] = array_merge($arr1[$keys[$v['rank']]], $v);

print_r($res);
splash58
  • 26,043
  • 3
  • 22
  • 34
0

Looking at your provided arrays, I'm assuming you want to use the key rank as the joining point (which doesn't seems such a good idea, and question will be if their unique or not) if they are unique then a tiny method can help to fetch element based on their rank and the rest is just assembling the result :

<?php

$arr1 = [
    [
        'rank' => 579,
        'id' => 1
    ],
    [
        'rank' => 251,
        'id' => 2
    ],
];

$arr2 = [
    [
        'size' => 'S',
        'rank' => 251
    ],
    [
        'size' => 'L',
        'rank' => 579
    ],
];

function getItemByRank($array, $rank)
{
    foreach ($array as $item){
        if ($item['rank'] === $rank) {
            return $item;
        }
    }
}


$result = [];
foreach ($arr1 as $k => $item) {

    $match = getItemByRank($arr2, $item['rank']);

    if (isset($match)) {
        $result[$k] = $item;
        $result[$k]['size'] = $match['size'];
    }
}

print_r($result);

output:

Array
(
    [0] => Array
        (
            [rank] => 579
            [id] => 1
            [size] => L
        )

    [1] => Array
        (
            [rank] => 251
            [id] => 2
            [size] => S
        )

)
Ali
  • 2,993
  • 3
  • 19
  • 42