Unique associative index from an array

Viewed 37

I have an array like this, i need to get the unique associative index

Array
(
    [0] => Array
        (
            [id] => 200
            [name] => james
            [place] => ca
         )

    [1] => Array
        (
            [id] => 201
            [name] => jana
            [place] => uk
         )

     [2] => Array
        (
            [id] => 203
            [name] => jana
            [place] => ca
         )
)

That means i need to get the unique 'place' from that array like

ca,uk

1 Answers

Make use of array_column() and array_unique()

array_unique(array_column($array, 'place'))
  • array_column — Return the values from a single column in the input array (PHP 5 >= 5.5.0, PHP 7)
  • array_unique — Removes duplicate values from an array (PHP 4 >= 4.0.1, PHP 5, PHP 7)
Related