How to filter a two dimensional array by value

PhpArrays

Php Problem Overview


How would I create a function that filters a two dimensional array by value?

Given the following array :

Array
(
    [0] => Array
        (
            [interval] => 2014-10-26
            [leads] => 0
            [name] => CarEnquiry
            [status] => NEW
            [appointment] => 0
        )

    [1] => Array
        (
            [interval] => 2014-10-26
            [leads] => 0
            [name] => CarEnquiry
            [status] => CALL1
            [appointment] => 0
        )

    [2] => Array
        (
            [interval] => 2014-10-26
            [leads] => 0
            [name] => Finance
            [status] => CALL2
            [appointment] => 0
        )

    [3] => Array
        (
            [interval] => 2014-10-26
            [leads] => 0
            [name] => Partex
            [status] => CALL3
            [appointment] => 0
        )

How would I filter the array to only show those arrays that contain a specific value in the name key? For example name = 'CarEnquiry'.

The resulting output would be:

Array
(
    [0] => Array
        (
            [interval] => 2014-10-26
            [leads] => 0
            [name] => CarEnquiry
            [status] => NEW
            [appointment] => 0
        )

    [1] => Array
        (
            [interval] => 2014-10-26
            [leads] => 0
            [name] => CarEnquiry
            [status] => CALL1
            [appointment] => 0
        )

    )

EDIT

I forgot to mention that the search value should be interchangeable - i.e. name = 'CarEnquiry' or name = 'Finance'.

Php Solutions


Solution 1 - Php

Use PHP's array_filter function with a callback.

$new = array_filter($arr, function ($var) {
    return ($var['name'] == 'CarEnquiry');
});

Edit: If it needs to be interchangeable, you can modify the code slightly:

$filterBy = 'CarEnquiry'; // or Finance etc.

$new = array_filter($arr, function ($var) use ($filterBy) {
    return ($var['name'] == $filterBy);
});

Solution 2 - Php

<?php

   

    function filter_array($array,$term){
        $matches = array();
        foreach($array as $a){
            if($a['name'] == $term)
                $matches[]=$a;
        }
        return $matches;
    }

    $new_array = filter_array($your_array,'CarEnquiry');

?>

Solution 3 - Php

If you want to make this a generic function use this:

function filterArrayByKeyValue($array, $key, $keyValue)
{
    return array_filter($array, function($value) use ($key, $keyValue) {
       return $value[$key] == $keyValue; 
    });
}

Solution 4 - Php

Above examples are using the exact word match, here is a simple example for filtering array to find imprecise "name" match.

  $options = array_filter($options, function ($option) use ($name) {
    return strpos(strtolower($option['text']), strtolower($name)) !== FALSE;
  });

Solution 5 - Php

array_filter is the function you need. http://php.net/manual/en/function.array-filter.php

Give it a filtering function like this:

function my_filter($elt) {
    return $elt['name'] == 'something';
}

Solution 6 - Php

function multi_array_search_with_condition($array, $condition)
{
    $foundItems = array();
    
    foreach($array as $item)
    {
        $find = TRUE;
        foreach($condition as $key => $value)
        {
            if(isset($item[$key]) && $item[$key] == $value)
            {
                $find = TRUE;
            } else {
                $find = FALSE;
            }
        }
        if($find)
        {
            array_push($foundItems, $item);
        }
    }
    return $foundItems;
}

This my function can use about this problem. You can use:

$filtered = multi_array_search_with_condition(
   $array,
   array('name' => 'CarEnquiry')
);

This will get your filtered items from your 2 dimensional array.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaphextwixView Question on Stackoverflow
Solution 1 - PhpdchestertonView Answer on Stackoverflow
Solution 2 - PhpksealeyView Answer on Stackoverflow
Solution 3 - PhpstackyView Answer on Stackoverflow
Solution 4 - PhpbpileView Answer on Stackoverflow
Solution 5 - PhpBenoît LatinierView Answer on Stackoverflow
Solution 6 - PhpMetin ErbekView Answer on Stackoverflow