0

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
        )
)
7
  • I don't understand. What happens when you have two identical names? How do you know which to choose? Commented Nov 27, 2012 at 18:18
  • I want to take every [name]of this array above and put into another array, but wihtout duplicates. Got it? Commented Nov 27, 2012 at 18:18
  • 1
    @PédeLeão Well... they're the same, so what does it matter? Commented Nov 27, 2012 at 18:18
  • @RocketHazmat Internal placeholder for record objects. Used by ADORecordSet->FetchObj(). Commented Nov 27, 2012 at 18:20
  • @LucasVeiga: This is an array of objects, not an array of arrays. Commented Nov 27, 2012 at 18:21

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

9 Comments

No problem @LucasVeiga happy to help ^_^
This is probably the most efficient way.
@netcoder yea... I do not understand all the if statements and array_unique's in the other answers... they add too much overhead :-P
Well this one is definitely the most elegant but not really the easiest to understand for a beginner imho ;)
@Intrepidd what is hard to understand? I will try to explain it better.
|
3
array_unique(array_map(function($element) {
    return $element->name;
}, $my_array));

1 Comment

+1 As far as I'm concerned, this is the only answer worth looking at. The standard library solves this problem without the need for a loop.
3

There you go

$res = array();

foreach($arr as $var)
{
    if(!in_array($var->name, $res))
    {
      $res[] = $var->name;
    }
}

2 Comments

$var is an object, not an array. Did anybody read this question properly before answering it?
Corrected, description was misleading : "I have an array inside of another array."
2

First, copy the names to a new array:

$arrNames = array();

foreach($arrOriginal as $objObject) {
    array_push(
        $arrNames,
        $objObject->name
    );
}

Then remove the duplicate names:

$arrNames = array_unique($arrNames);

Comments

0
$n = array();

foreach($array as $d) $n[] = $d->name;

$n = array_unique($n);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.