0

我有一个对象数组。

我需要将[name]每个对象放入另一个数组中,但我不想重复。

我该怎么做?

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
        )
)

谢谢!

4

5 回答 5

5

你可以这样做:

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

这将起作用,因为根据定义,数组键必须是唯一的。

于 2012-11-27T18:17:54.320 回答
3
array_unique(array_map(function($element) {
    return $element->name;
}, $my_array));
于 2012-11-27T18:17:08.517 回答
3

给你

$res = array();

foreach($arr as $var)
{
    if(!in_array($var->name, $res))
    {
      $res[] = $var->name;
    }
}
于 2012-11-27T18:17:10.067 回答
2

首先,将名称复制到一个新数组中:

$arrNames = array();

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

然后删除重复的名称:

$arrNames = array_unique($arrNames);
于 2012-11-27T18:16:51.223 回答
0
$n = array();

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

$n = array_unique($n);
于 2012-11-27T18:28:04.010 回答