PHP filter 2 dimensional array by specific key

I have this array:

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

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

        )

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

I want to filter the array so that only the "folder" type remains:

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

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

        )

)

I could do this, but I will need a general function:

$temp = array();
foreach($array as $key => $value)
{
    if($value['type'] =="folder")
    {
        $temp[$key] = $value; 
    }
}
+3
source share
2 answers

You can use array_filter:

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

All Articles