Get unique value of one attribute from array of associative arrays

后端 未结 5 589
青春惊慌失措
青春惊慌失措 2021-01-18 06:38

I have an array like this:

$a = array(
    0 => array(\'type\' => \'bar\', \'image\' => \'a.jpg\'),
    1 => array(\'type\' => \'food\', \'ima         


        
5条回答
  •  再見小時候
    2021-01-18 07:16

    Using PHP >= 5.5, you could do:

    $ar = array_unique(array_column($a, 'type'));
    

    print_r($ar):

    Array ( 
        [0] => bar 
        [1] => food 
        [3] => default 
    )
    

    http://php.net/manual/en/function.array-column.php

    http://php.net/manual/en/function.array-unique.php

提交回复
热议问题