根据密钥php比较两个数组

时间:2022-05-22 12:27:56

I have two arrays and i want to extract only the values from the second array only if its key matches the first array key.

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

My first array is

我的第一个阵列是

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

    )

My second array is

我的第二个阵列是

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

    )

Expected output:

预期产量:

[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

                )

        )

I want only those values from the second array whose key matches the first array. I want my second array to look exactly like my first except the values.

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

Thanks

谢谢

4 个解决方案

#1


0  

I think that is best done with some for loops. Will not be that efficient, but as far as I know there is no real automated way of doing this.

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

I'm not sure if this is exactly what you want, but it may be a start. So don't just copy past the code (because it will probably not work because it isn't tested), but maybe it is a good start for your own code.

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

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
        }
    }
}

#2


0  

I try to replicate your array into this:

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

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

and process it using this snippet:

并使用此代码段处理它:

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

NOTE: The real trick here is, by using array_key_exists() to check if the key in question exists in $package array.

注意:这里真正的技巧是,通过使用array_key_exists()来检查$ package数组中是否存在有问题的密钥。

With the following results:

结果如下:

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

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

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

)

#3


0  

Thanks all for your help. Taking clues from the suggestion above. I finally got the answer.

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

here is the working code:

这是工作代码:

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

#4


0  

Just use this array_intersect_key by looping into an array that is small.

只需通过循环到一个小的数组来使用这个array_intersect_key。

#1


0  

I think that is best done with some for loops. Will not be that efficient, but as far as I know there is no real automated way of doing this.

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

I'm not sure if this is exactly what you want, but it may be a start. So don't just copy past the code (because it will probably not work because it isn't tested), but maybe it is a good start for your own code.

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

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
        }
    }
}

#2


0  

I try to replicate your array into this:

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

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

and process it using this snippet:

并使用此代码段处理它:

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

NOTE: The real trick here is, by using array_key_exists() to check if the key in question exists in $package array.

注意:这里真正的技巧是,通过使用array_key_exists()来检查$ package数组中是否存在有问题的密钥。

With the following results:

结果如下:

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

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

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

)

#3


0  

Thanks all for your help. Taking clues from the suggestion above. I finally got the answer.

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

here is the working code:

这是工作代码:

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

#4


0  

Just use this array_intersect_key by looping into an array that is small.

只需通过循环到一个小的数组来使用这个array_intersect_key。