skip to Main Content

The following works great for me in filtering out my multidimensional array down to the elements I want to focus on…

$mAllLists = []; //my big array
$mFilteredLists = array_filter($mAllLists, function ($data)
{
    return $data['stringid'] == "dog" || $data['stringid'] == 'people'
    || $data['stringid'] == 'house' || $data['stringid'] == 'cat';
});

But is possible with array_filter, we can replace $data with a real array that has the keys we want to match against? or is there a better PHP function for that kind of process?

For example, if I had an array that looks like this…

array (
  0 => 
  array (
    'contactList' => 
    array (
      'stringid' => 'dog',
      'status' => '1',
      'sourceid' => '0',
    ),
  ),
  1 => 
  array (
    'contactList' => 
    array (
      'stringid' => 'people',
      'status' => '1',
      'sourceid' => '0',
    ),
  ),
)

I would like to use the stringid keys instead of hard coding the $data values for my return in $mFilteredLists.

Is this possible?

2

Answers


  1. Chosen as BEST ANSWER

    After some deep researching and testing things out, I come to the conclusion that it would of been beneficial for me to rename this thread as "How to filter a multidimensional array by multiple key/values of another array".

    With that, I found array_walk with a callback useful for my goal. the following example brings me to my goal completely...

        $myListDetails = []; //my multidemensional array holding all data....
        $allowed = [array(array('stringid' => 'cat'), array('stringid' => 'dog'))];
        $output = [];
        array_walk($myListDetails, function ($element) use ($allowed, &$output)
        {
            $matches = true;
            foreach ($allowed as $allow)
            {
                foreach ($allow as $subval)
                {
                    if (!in_array($subval, $element))
                    {
                        $matches = false;
                    }
                    else
                    {
                        $output[] = $element;
                    }
                }
            }
        });
    
        //displays a new array of my desired elements
        echo '<pre>'  .  var_export($ouput, true) .  '</pre>';
       
    
    

  2. The most basic solution is:

    const VALID_IDS = ['dog', 'people', 'house'];
    
    $mFilteredLists = array_filter($mAllLists, function ($data)
    {
        return in_array($data['stringid'], VALID_IDS);
    });
    

    Though in_array is somewhat slow, an alternative method is:

    const VALID_IDS = ['dog'=>1, 'people'=>1, 'house'=>1];
    
    $mFilteredLists = array_filter($mAllLists, function ($data)
    {
        return isset(VALID_IDS[$data['stringid']]);
    });
    

    If VALID_IDS has to be a variable:

    $VALID_IDS = ['dog', 'people', 'house'];
    $VALID_IDS = array_flip($VALID_IDS);
    
    $mFilteredLists = array_filter($mAllLists,
                      fn ($data) => isset($VALID_IDS[$data['stringid']]));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search