phparraysmultidimensional-arrayuniquearray-column

Get unique column values from an array of objects


I have an array of objects.

What I need is to take each [name] of each object in put into another array, but I don't want duplicates.

How can I do it?

Array (
    [0] => ADOFetchObj Object
        (
            [name] => Team 1
            [att] => None
            [idGrupo] => 1
            [idModulo] => 4
            [ler] => 1
            [escrever] => 1
            [excluir] => 1
        )

    [1] => ADOFetchObj Object
        (
            [name] => Team 1
            [nomeModulo] => Aplicar Juros
            [idGrupo] => 1
            [idModulo] => 1006
            [ler] => 1
            [escrever] => 1
            [excluir] => 1
        )

    [2] => ADOFetchObj Object
        (
            [name] => Team 2
            [att] => None
            [idGrupo] => 1
            [idModulo] => 10
            [ler] => 1
            [escrever] => 1
            [excluir] => 1
        )

    [3] => ADOFetchObj Object
        (
            [name] => Team 2
            [att] => None
            [idGrupo] => 1
            [idModulo] => 1012
            [ler] => 1
            [escrever] => 1
            [excluir] => 1
        )
)

Solution

  • You can do this:

    $names = array();
    foreach($arr as $list) {
        $names[$list->name] = true; // can be *any* arbitrary value
    }
    $names = array_keys($names);
    

    This will work because by definition array keys have to be unique.