-1

This is my array:

$myArray = array(
    array("name"=>"Andrea", "Age"=>17),
    array("name"=>"Tresna", "Age"=>20),
    array("name"=>"Aria", "Age"=>12)
);

I want to filter that multi array by value in array. This is my filter:

$filter = array("Andrea", "Aria");

So the result must be like this:

$newArray = array(
    array("name"=>"Andrea", "Age"=>17),
    array("name"=>"Aria", "Age"=>12)
);

how to do like that?

Dimas Adi Andrea
  • 443
  • 3
  • 11
  • 25
  • Instead of filtering on a whitelist, here is filtering with the same structures, but with a blacklist: [Filter 2d array by a flat, blacklist array](https://stackoverflow.com/q/50784166/2943403) – mickmackusa Mar 30 '23 at 22:28

2 Answers2

5

The hint is already there, to filter, use array_filter.

Don't forget to use use keyword to import your criteria.

Example:

$newArray = array_filter($myArray, function($e) use ($filter){
                                                //    ^ import criteria
    return in_array($e['name'], $filter);
});
Kevin
  • 41,694
  • 12
  • 53
  • 70
0
$myArray = array(
    array("name"=>"Andrea", "Age"=>17),
    array("name"=>"Tresna", "Age"=>20),
    array("name"=>"Aria", "Age"=>12)
);
$filter = array("Andrea", "Aria");

foreach($myArray as $arr)
{

    foreach($filter as $value)
    {
        if(in_array($value,$arr))
        {
            $finalArr[]=$arr;
        }
    }

}

$finalArr is your result

Ekta Puri
  • 306
  • 2
  • 8