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
CodePudding user response:
$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
CodePudding user response:
Check the PHP array_intersect() function that should do what you want.
