Home » Php » php – How to filter a two dimensional array by value

php – How to filter a two dimensional array by value

Posted by: admin April 23, 2020 Leave a comment

Questions:

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'.

How to&Answers:

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);
});

Answer:

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';
}

Answer:

<?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');

?>

Answer:

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; 
    });
}

Answer:

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.

Answer:

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;
  });