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"
5
  • 4
    array_merge ? Commented Jul 2, 2015 at 7:17
  • the desired output doesn't really make much sense to me. Commented Jul 2, 2015 at 7:22
  • Have you tried anything? How are you getting those arrays? Commented Jul 2, 2015 at 7:22
  • 1
    I bet you are doing something wrong with those arrays. Commented Jul 2, 2015 at 7:26
  • Agree with @RoboRobok, if you explain the problem a bit wider there might be a better approach to prevent you to get to this point in first place :) Commented Jul 2, 2015 at 9:29

5 Answers 5

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']];
        };
    };
};
Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
1
foreach($arr1 as $key1 => $data1){
    foreach($arr2 as $key2 => $data2){
        if($data1['rank']==$data2['rank']){
            $result[] = array_merge($data1, $data2);
        }
    }
}
print_r($result);

3 Comments

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 :) )
if you have any unique key mens you used that one otherwise you don't need to check if condion and seconf for loop. :)
Agreed, as far our assumption around the rank is valid ;)
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);

Comments

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
        )

)

Comments

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);

2 Comments

What if the sequences are not same?
if what most of answers assumed is the case and @user3686600 want to use the rank as a joining point the this solution will be incorrect

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.