简体   繁体   中英

filter multidimensional associative array with multidimensional associative array in php

i have two multidimensional associative arrays, but i want to get data that only have similarities. this is an example of my data array.

$data = array(
           array("id"=>"1","dress_color"=>"Red","size" => "L"),
           array("id"=>"2","dress_color"=>"Blue","size"  => "L"),
           array("id"=>"3","dress_color"=>"Blue","size"  => "S")
        );

I want to filter the associative array with a similar array.

$match = array(
            array("dress_color"=>"Red","size" => "L"),
            array("dress_color"=>"Blue","size" => "L")
        );

and the output I expect the data to be is this.

$expected = array(
           array("id"=>"1","dress_color"=>"Red","size" => "L"),
           array("id"=>"2","dress_color"=>"Blue","size"  => "L"),
        );

I hope for help from all my colleagues, I'm having trouble with this part

$filtered = array_filter($data, function($input) use ($match) {
                $arrayData = [];
                foreach ($match as $value) {
                    if($input["dress_color"] === $value["dress_color"] && $input["size"] === $value["size"]){
                        $arrayData[] = $input["dress_color"] === $value["dress_color"] && $input["size"] === $value["size"];
                    }
                }
                return $arrayData;
            });

i trying, and success

Check the PHP array_intersect() function that should do what you want.

https://www.php.net/manual/en/function.array-intersect.php

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