比较不同的阵列结构

时间:2018-06-04 08:57:43

标签: php arrays multidimensional-array

我有两个不同结构的数组。我将从MyListMyFiles命名的数组1和数组2。我只能返回MyFiles中没有的MyList值。但是这两个阵列有不同的结构,我在尝试比较

时遇到了麻烦

MYLIST

Array
(
    [info] => Array
        (
            [0] => Array
                (
                    [player] => Messi
                    [week] => Array
                        (
                            [id] => 252
                            [videos] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2929850
                                            [link] => goals.mp4
                                        )

                                    [1] => Array
                                        (
                                            [id] => 2929848
                                            [link] => best.mp4
                                        )

                                    [2] => Array
                                        (
                                            [id] => 2929847
                                            [link] => dribbling.mp4
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [player] => CR7
                    [week] => Array
                        (
                            [id] => 251
                            [videos] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2929796
                                            [link] => goals.mp4
                                        )

                                    [1] => Array
                                        (
                                            [id] => 2929795
                                            [link] => best.mp4
                                        )

                                )

                        )

                )

            [2] => Array
                (
                    [player] => Neymar
                    [week] => Array
                        (
                            [id] => 253
                            [videos] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2929794
                                            [link] => goals.mp4
                                        )

                                    [1] => Array
                                        (
                                            [id] => 2929793
                                            [link] => best.mp4
                                        )

                                )

                        )

                )

        )

)

MyFiles数组

Array
(
    [252] => Array
        (
            [0] => Array
                (
                    [id] => 2929850
                    [link] => goals.mp4
                )

            [1] => Array
                (
                    [id] => 2929848
                    [link] => best.mp4
                )

        )

    [251] => Array
        (
            [0] => Array
                (
                    [id] => 2929796
                    [link] => goals.mp4
                )

            [1] => Array
                (
                    [id] => 2929795
                    [link] => best.mp4
                )

        )

)

必须通过周的ID和视频的ID来进行比较

我尝试了这个但是没有成功:

$new = array();
    foreach ($list['info'] as $source) {

        foreach ($source["week"]['videos'] as $keys => $videos) {


            foreach ($file as $key => $upload) {

                if ($source["week"]["id"] == $key ) {
                    for($i=0; $i<count($source["week"]["videos"]); $i++){

                        if($videos["id"] == $upload[$i]["id"]){
                            unset($videos);

                            }else{

                            $new[] = $videos;
                        }
                    }

                    } else {

                    $new[] = $videos;
                }

            }

        }
    }

预期回报将是这样的。

Array
(
    [info] => Array
        (
            [0] => Array
                (
                    [player] => Messi
                    [week] => Array
                        (
                            [id] => 252
                            [videos] => Array
                                (
                                   [2] => Array
                                        (
                                            [id] => 2929847
                                            [link] => dribbling.mp4
                                        )

                                )

                        )

                )
            [2] => Array
                (
                    [player] => Neymar
                    [week] => Array
                        (
                            [id] => 253
                            [videos] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2929794
                                            [link] => goals.mp4
                                        )

                                    [1] => Array
                                        (
                                            [id] => 2929793
                                            [link] => best.mp4
                                        )

                                )

                        )

                )

        )

)

1 个答案:

答案 0 :(得分:1)

如果这个答案不正确并且需要更改,我已经将数组隐藏在一个可用的格式中,以备将来使用。

&#13;
&#13;
$desired = array();
$desired['info'][0]['player'] = 'Messi';
$desired['info'][0]['week']['id'] = 252;
$desired['info'][0]['week']['videos'][2]['id'] = 2929847;
$desired['info'][0]['week']['videos'][2]['link'] = 'dribbling.mp4';
$desired['info'][2]['player'] = 'Neymar';
$desired['info'][2]['week']['id'] = 253;
$desired['info'][2]['week']['videos'][0]['id'] = 2929794;
$desired['info'][2]['week']['videos'][0]['link'] = 'goals.mp4';
$desired['info'][2]['week']['videos'][1]['id'] = 2929793;
$desired['info'][2]['week']['videos'][1]['link'] = 'best.mp4';

$list = array();
$list["info"][0]["player"] = "Messi";
$list["info"][0]["week"]["id"] = "252";
$list["info"][0]["week"]["videos"][0]["id"] = 2929850;
$list["info"][0]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][0]["week"]["videos"][1]["id"] = 2929848;
$list["info"][0]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][0]["week"]["videos"][2]["id"] = 2929847;
$list["info"][0]["week"]["videos"][2]["link"] = "dribbling.mp4";
$list["info"][1]["player"] = "CR7";
$list["info"][1]["week"]["id"] = "251";
$list["info"][1]["week"]["videos"][0]["id"] = 2929796;
$list["info"][1]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][1]["week"]["videos"][1]["id"] = 2929795;
$list["info"][1]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][2]["player"] = "Neymar";
$list["info"][2]["week"]["id"] = "253";
$list["info"][2]["week"]["videos"][0]["id"] = 2929794;
$list["info"][2]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][2]["week"]["videos"][1]["id"] = 2929793;
$list["info"][2]["week"]["videos"][1]["link"] = "best.mp4";

$file = array();
$file[252][0]['id'] = 2929850;
$file[252][0]['link'] = 'goals.mp4';
$file[252][1]['id'] = 2929848;
$file[252][1]['link'] = 'best.mp4';
$file[251][0]['id'] = 2929796;
$file[251][0]['link'] = 'goals.mp4';
$file[251][1]['id'] = 2929795;
$file[251][1]['link'] = 'best.mp4';
&#13;
&#13;
&#13;

修改

function array_diff_assoc_recursive($array1, $array2) {
    $difference=array();
    foreach ($array1 as $key => $value) {
        if (is_array($value)) {
            if( !isset($array2[$key]) || !is_array($array2[$key])) {
                $difference[$key] = $value;
            } else {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if (!empty($new_diff))
                    $difference[$key] = $new_diff;
            }
        } else if (!array_key_exists($key,$array2) || $array2[$key] !== $value) {
            $difference[$key] = $value;
        }
    }
    return $difference;
}

$new = array('info' => array());
foreach ($list['info'] as $key => $item) {
    $a = $item['week']['videos'];
    //$b = $file[$item['week']['id']] ?? []; // This is PHP7+
    $b = isset($file[$item['week']['id']]) ? $file[$item['week']['id']] : [];
    $c = array_diff_assoc_recursive($a, $b);

    if (!empty($c)) {
        $new['info'][$key] = $item;
        $new['info'][$key]['week']['videos'] = $c;
    }
}

您将需要一个能够检查视频阵列之间差异的功能。

我所做的只是迭代列表数组,然后检查该项与文件数组之间的区别。然后将差异存储在$c

如果存在差异,则触发if语句,将该播放器存储在$new数组中,然后将视频数组替换为差异数组。
这与您在取消设置变量时的操作类似。