php 如何按值过滤二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27447923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:22:14  来源:igfitidea点击:

How to filter a two dimensional array by value

phparrays

提问by aphextwix

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 namekey? For example name = 'CarEnquiry'.

我将如何过滤数组以仅显示那些包含name键中特定值的数组?例如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'.

我忘了提到搜索值应该是可互换的 - 即name = 'CarEnquiry'name = 'Finance'.

回答by dchesterton

Use PHP's array_filterfunction with a callback.

将 PHP 的array_filter函数与回调一起使用。

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

回答by stacky

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

回答by Beno?t Latinier

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

array_filter是你需要的功能。http://php.net/manual/en/function.array-filter.php

Give it a filtering function like this:

给它一个这样的过滤功能:

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

回答by ksealey

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

?>

回答by Metin Erbek

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.

这将从您的二维数组中获取您的过滤项目。

回答by bpile

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