简体   繁体   中英

Compare different arrays structures

I have two arrays with different structures. Array 1 and Array 2 that I will name from MyList and MyFiles . I would get to return only MyList values that do not have in MyFiles. But the two arrays have different structures and I'm having trouble trying to compare

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

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
                )

        )

)

the comparison must be made by id of the week and id of the video

I tried this but it did not work out:

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

            }

        }
    }

The expected return would be something like this.

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
                                        )

                                )

                        )

                )

        )

)

I have hidden the array in a usable format for my sake in the future should this answer be incorrect and need changing.

 $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'; 

Edit

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

You will need a function that will check the difference between the videos arrays.

What I do is simply iterate over the list array and then check the difference between that item and the file array. The difference is then stored in $c .

If there is a difference then if statement is fired which will store that player in the $new array and then replace the videos array with the difference array.
This is similar to what you were doing when you were unsetting variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM