PHP过滤二维数组的特定关键字



我有这样一个数组:

Array
(
    [702a4584] => Array
        (
            [type] => folder
            [id] => 702a4584
        )
    [b547b3a9] => Array
        (
            [type] => folder
            [id] => b547b3a9
        )
    [fcb0d055] => Array
        (
            [type] => page
            [id] => fcb0d055
        )
)

我想对数组进行过滤,这样只会保留"folder"类型:

Array
(
    [702a4584] => Array
        (
            [type] => folder
            [id] => 702a4584
        )
    [b547b3a9] => Array
        (
            [type] => folder
            [id] => b547b3a9
        )
)

我可以这样做,但我需要一个泛型函数:

$temp = array();
foreach($array as $key => $value)
{
    if($value['type'] =="folder")
    {
        $temp[$key] = $value; 
    }
}

您可以使用array_filter:

$filtered = array_filter($array, function($v) { return $v['type'] == 'folder'; });
$input = Array(1,2,3,1,2,3,4,5,6);
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

相关内容

  • 没有找到相关文章

最新更新