如何将两个多维数组相交成第三个多维数组



我有2个数组,我希望它们相交并将查找匹配存储到具有第一个数组和第二个数组值的第三个数组中。第一个数组看起来像这样:

Array
(
    [0] => Array
        (
            [0] => 45
            [1] => 10640
            [2] => 1041-0567041700116
        )
    [1] => Array
        (
            [0] => 46
            [1] => 10640
            [2] => 1041-0567041700318
        )
    [2] => Array
        (
            [0] => 207
            [1] => 10645
            [2] => 03320103000052
        )

和第二个数组:

Array
(
    [0] => Array
        (
            [0] => 03320103000052
            [1] => 0
        )
    [1] => Array
        (
            [0] => 10013800805001
            [1] => 12
        )
    [2] => Array
        (
            [0] => 1090-0360141758201
            [1] => 3
        )

输出应该是:

Array
(
        [0] => Array
            (
                [0] => 207                     =>value from first array
                [1] => 10645                   =>value from first array
                [2] => 03320103000052          =>value from first and second array (this is what i need to compare)
                [3] => 0                       =>value from second array
            )

这和这篇文章很相似

,但我有问题,以存储数据到多维数组

感谢您的任何建议和帮助

只需要两个foreach循环和一个if语句:

$combined = array();
foreach ($array1 as $a) {
    foreach ($array2 as $b) {
        if ($a[2] == $b[0]) {
            $combined[] = array($a[0], $a[1], $a[2], $b[1]);
        }
    }
}

下面是我设置的测试:

<?php
$array1 = array();
$array1[] = array('45', '10640', '1041-0567041700116');
$array1[] = array('46', '10640', '1041-0567041700318');
$array1[] = array('207', '10645', '03320103000052');
$array2 = array();
$array2[] = array('03320103000052', '0');
$array2[] = array('10013800805001', '12');
$array2[] = array('1090-0360141758201', '3');

$combined = array();
foreach ($array1 as $a) {
    foreach ($array2 as $b) {
        if ($a[2] == $b[0]) {
            $combined[] = array($a[0], $a[1], $a[2], $b[1]);
        }
    }
}
print_r($combined);
?>

相关内容

  • 没有找到相关文章

最新更新