Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get result from multidimensional array without duplicate results in php

Tags:

arrays

php

I have an array, and would like to get all the values like this [*][place], without duplicate results. The output should be looking like this :

Sønderjylland Nordjylland Sjælland

Array
    (
        [0] => Array
            (
                [place] => Sønderjylland
                [active] => Lagerarbejde
                [num] => 123
            )

        [1] => Array
            (
                [place] => Nordjylland
                [active] => Tømrer
                [num] => 124
            )

        [2] => Array
            (
                [place] => Sønderjylland
                [active] => Klejnsmed
                [num] => 125
            )

        [3] => Array
            (
                [place] => Sjælland
                [active] => Elektriker
                [num] => 126
            )
    )
like image 794
Issam Mousleh Avatar asked Nov 22 '25 07:11

Issam Mousleh


1 Answers

You can use array_column, array_unique, array_filter, implode together.

echo implode(' ', array_filter(array_unique(array_column($yourArray, 'place'))));

array_column is supported for (PHP 5 >= 5.5.0, PHP 7)

If you are using older versions then a loop would help.

like image 74
Sougata Bose Avatar answered Nov 24 '25 21:11

Sougata Bose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!