根据密钥php比较两个数组

时间:2012-12-19 10:45:48

标签: php

我有两个数组,我只想从第二个数组中提取值,只要它的键与第一个数组键匹配。

我的第一个数组是

 [deal_id] => Array
    (
        [0] => Array
            (
                [0] => 75
                [5] => 76
                [10] => 77
                [15] => 79
            )

        [1] => Array
            (
                [0] => 84
            )

        [2] => Array
            (
                [0] => 86
                [3] => 88
                [8] => 89
            )

        [3] => Array
            (
                [0] => 97
            )

        [4] => Array
            (
                [0] => 100
                [4] => 104
            )

    )

我的第二个数组是

 [package_id] => Array
    (
        [0] => Array
            (
                [0] => 10
                [1] => 75
                [2] => 75
                [3] => 75
                [4] => 75
                [5] => 67
                [6] => 34
                [7] => 89
                [10] => 04
                [15] => 75
            )

        [1] => Array
            (
                [0] => 10
                [1] => 29
                [2] => 34
                [3] => 45
            )

        [2] => Array
            (
                [0] => 12
                [3] => 23
                [4] => 45
                [5] => 76
                [6] => 87
                [8] => 45
            )

        [3] => Array
            (
                [0] => 34
                [1] => 54
                [2] => 34

            )

        [4] => Array
            (
                [0] => 145
                [1] => 143
                [6] => 146
                [4] => 344
            )

    )

预期产出:

[package_id] => Array
        (
            [0] => Array
                (
                    [0] => 10
                    [5] => 67
                    [10] => 04
                    [15] => 75
                )

            [1] => Array
                (
                    [0] => 10

                )

            [2] => Array
                (
                    [0] => 12
                    [3] => 23
                    [8] => 45
                )

            [3] => Array
                (
                    [0] => 34

                )

            [4] => Array
                (
                    [0] => 145
                    [4] => 344

                )

        )

我只想要第二个数组中那些键匹配第一个数组的值。我希望我的第二个数组看起来与我的第一个数组完全相同,除了值。

由于

4 个答案:

答案 0 :(得分:0)

我认为最好用一些for循环来完成。效率不高,但据我所知,没有真正的自动化方式。

我不确定这是否正是你想要的,但它可能是一个开始。所以不要只是复制过去的代码(因为它可能不会工作,因为它没有经过测试),但也许这对你自己的代码来说是一个好的开始。

for($arr1 as $key11 => $val11){
    if(isset($arr2[$key11])){
        $contains = false;
        for($val11 as $key12 => $val12){
            if(isset($arr2[$key11][$key12]){
                // Contains
                $contains = true;
            }
        }
        if(!$contains){
            // Doesn't contain
        }
    }
}

答案 1 :(得分:0)

我尝试将您的数组复制到此:

$deal = array(
    array(
        0=>75, 5=>76, 10=>77, 15=>79,
    ),
    array(
        0=>84,
    ),
    array(
        0=>86, 3=>88, 8=>89,
    ),
);

$package = array(
    array(
        0=>10, 1=>75, 2=>75, 3=>75, 4=>75, 5=>67, 6=>34, 7=>89, 10=>04, 15=>75,
    ),
    array(
        0=>10, 1=>29, 2=>34, 3=>45,
    ),
    array(
        0=>12, 3=>23, 4=>45, 5=>76, 6=>87, 8=>45,
    ),
);

并使用此代码段处理它:

$result = array();

foreach($deal as $key => $value){
    foreach($value as $key2 => $value2){
        if(array_key_exists($key2, $package[$key])){
            if(!isset($key, $result)){
                $result[$key] = array();
            }
            $result[$key][$key2] = $package[$key][$key2];
        }
    }
}
  

注意:这里的真正诀窍是,使用array_key_exists()检查$package数组中是否存在相关密钥。

得到以下结果:

Array
(
    [0] => Array
        (
            [0] => 10
            [5] => 67
            [10] => 4
            [15] => 75
        )

    [1] => Array
        (
            [0] => 10
        )

    [2] => Array
        (
            [0] => 12
            [3] => 23
            [8] => 45
        )

)

答案 2 :(得分:0)

感谢大家的帮助。从上面的建议中获取线索。我终于得到了答案。

这是工作代码:

 foreach ($deal_ids as $dealkey => $dealval) {
            foreach ($dealval as $dkey => $dval) {
                foreach ($deal_duration as $dukey => $duval) {
                    foreach ($duval as $deukey => $deuval) {

                        if ($dkey == $deukey) {
                            $dur[$dealkey][$deukey] = $deuval;
                        }
                    }
                }
            }
        }

答案 3 :(得分:0)

只需使用此array_intersect_key循环到一个小的数组。