PHP按值为2个不同的数组值合并数组

时间:2022-10-11 12:15:46

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 个解决方案

#1


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']];
        };
    };
};

#2


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


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

#4


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
        )

)

#5


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.

虽然我不明白为什么你想以这种方式合并数组,但这是一种通过序列合并数组的方法。所以array1的第一个子节点将与array2的第一个子节点等合并。

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

#1


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']];
        };
    };
};

#2


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


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

#4


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
        )

)

#5


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.

虽然我不明白为什么你想以这种方式合并数组,但这是一种通过序列合并数组的方法。所以array1的第一个子节点将与array2的第一个子节点等合并。

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