开发者

PHP filter 2 dimensional array by specific key

开发者 https://www.devze.com 2023-04-03 03:58 出处:网络
I have this array: Array ( [702a4584] => Array ( 开发者_Go百科[type] => folder [id] => 702a4584

I have this array:

Array
(
    [702a4584] => Array
        (
          开发者_Go百科  [type] => folder
            [id] => 702a4584
        )

    [b547b3a9] => Array
        (
            [type] => folder
            [id] => b547b3a9

        )

    [fcb0d055] => Array
        (
            [type] => page
            [id] => fcb0d055
        )
)

I want to filter the array such that only type "folder" will remain:

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

    [b547b3a9] => Array
        (
            [type] => folder
            [id] => b547b3a9

        )

)

I could do this, but I'll be needing a generic function:

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


You could use 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)));
0

精彩评论

暂无评论...
验证码 换一张
取 消