Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于对象属性从数组中删除重复项继续_Php_Arrays_Object - Fatal编程技术网

Php 基于对象属性从数组中删除重复项继续

Php 基于对象属性从数组中删除重复项继续,php,arrays,object,Php,Arrays,Object,有关: 有没有办法做到这一点,但考虑顺序?例如,如果我不想保留[2],而是想保留[1] [0]=> object(stdClass)#337 (9) { ["term_id"]=> string(2) "23" ["name"]=> string(12) "Assasination" ["slug"]=> string(12) "assasination" } [1]=> object(stdCla

有关:

有没有办法做到这一点,但考虑顺序?例如,如果我不想保留[2],而是想保留[1]

  [0]=>
  object(stdClass)#337 (9) {
    ["term_id"]=>
    string(2) "23"
    ["name"]=>
    string(12) "Assasination"
    ["slug"]=>
    string(12) "assasination"
  }
  [1]=>
  object(stdClass)#44 (9) {
    ["term_id"]=>
    string(2) "14"
    ["name"]=>
    string(16) "Campaign Finance"
    ["slug"]=>
    string(16) "campaign-finance"
  }
  [2]=>
  object(stdClass)#298 (9) {
    ["term_id"]=>
    string(2) "15"
    ["name"]=>
    string(16) "Campaign Finance"
    ["slug"]=>
    string(49) "campaign-finance-good-government-political-reform"
  }

我试图发表评论,但由于我的声誉,它不允许我发表评论,所以我决定开始一个新的帖子。这应该为每个
name
属性保留第一个对象,并删除其余的对象

$unique = [];

foreach ($array as $key => $object) {

    if (!isset($unique[$object->name])) {
        // this will add each name to the $unique array the first time it is encountered
        $unique[$object->name] = true;
    } else {
        // this will remove all subsequent objects with that name attribute
        unset($array[$key]);
    }
}

我在
$unique
数组中将对象名用作键而不是值,因此可以使用
isset
来检查现有名称,这应该比将名称添加为值时必须使用的
数组中的
快。这应该为每个
name
属性保留第一个对象,并删除其余的对象

$unique = [];

foreach ($array as $key => $object) {

    if (!isset($unique[$object->name])) {
        // this will add each name to the $unique array the first time it is encountered
        $unique[$object->name] = true;
    } else {
        // this will remove all subsequent objects with that name attribute
        unset($array[$key]);
    }
}

我在
$unique
数组中将对象名用作键而不是值,因此
isset
可用于检查现有名称,这应该比将名称添加为值时必须使用的
数组中的
快。

反转和数组很简单。因此,在处理数组之前,对其调用
array\u reverse()

/** flip it to keep the last one instead of the first one **/
$array = array_reverse($array);
最后,如果订购存在问题,您可以再次将其反转:

/** Answer Code ends here **/
/** flip it back now to get the original order **/
$array = array_reverse($array);
因此,将所有这些放在一起看起来如下:

class my_obj
{
    public $term_id;
    public $name;
    public $slug;

    public function __construct($i, $n, $s)
    {
            $this->term_id = $i;
            $this->name = $n;
            $this->slug = $s;
    }
}

$objA = new my_obj(23, "Assasination", "assasination");
$objB = new my_obj(14, "Campaign Finance", "campaign-finance");
$objC = new my_obj(15, "Campaign Finance", "campaign-finance-good-government-political-reform");

$array = array($objA, $objB, $objC);

echo "Original array:\n";
print_r($array);

/** flip it to keep the last one instead of the first one **/
$array = array_reverse($array);

/** Answer Code begins here **/

// Build temporary array for array_unique
$tmp = array();
foreach($array as $k => $v)
    $tmp[$k] = $v->name;

// Find duplicates in temporary array
$tmp = array_unique($tmp);

// Remove the duplicates from original array
foreach($array as $k => $v) {
  if (!array_key_exists($k, $tmp))
    unset($array[$k]);
}

/** Answer Code ends here **/
/** flip it back now to get the original order **/
$array = array_reverse($array);

echo "After removing duplicates\n";
echo "<pre>".print_r($array, 1);

反转和数组是很简单的。因此,在处理数组之前,对其调用
array\u reverse()

/** flip it to keep the last one instead of the first one **/
$array = array_reverse($array);
最后,如果订购存在问题,您可以再次将其反转:

/** Answer Code ends here **/
/** flip it back now to get the original order **/
$array = array_reverse($array);
因此,将所有这些放在一起看起来如下:

class my_obj
{
    public $term_id;
    public $name;
    public $slug;

    public function __construct($i, $n, $s)
    {
            $this->term_id = $i;
            $this->name = $n;
            $this->slug = $s;
    }
}

$objA = new my_obj(23, "Assasination", "assasination");
$objB = new my_obj(14, "Campaign Finance", "campaign-finance");
$objC = new my_obj(15, "Campaign Finance", "campaign-finance-good-government-political-reform");

$array = array($objA, $objB, $objC);

echo "Original array:\n";
print_r($array);

/** flip it to keep the last one instead of the first one **/
$array = array_reverse($array);

/** Answer Code begins here **/

// Build temporary array for array_unique
$tmp = array();
foreach($array as $k => $v)
    $tmp[$k] = $v->name;

// Find duplicates in temporary array
$tmp = array_unique($tmp);

// Remove the duplicates from original array
foreach($array as $k => $v) {
  if (!array_key_exists($k, $tmp))
    unset($array[$k]);
}

/** Answer Code ends here **/
/** flip it back now to get the original order **/
$array = array_reverse($array);

echo "After removing duplicates\n";
echo "<pre>".print_r($array, 1);
您可以通过以下方式完成:

基本上,我们遍历一个对象数组并跟踪使用过的名称。如果名称还没有使用,我们将其添加到used中,并将当前对象添加到结果中

这是。

您可以通过以下方式完成:

基本上,我们遍历一个对象数组并跟踪使用过的名称。如果名称还没有使用,我们将其添加到used中,并将当前对象添加到结果中


这是。

@sevavietl我对原始数组进行了变异,因为这似乎是这个问题所基于的链接问题的目标
$unique
将只保存
$array
中的每个
名称
属性作为键,循环后,任何具有重复名称的对象都将被删除。啊,不用担心。@sevavietl我对原始数组进行了变异,因为这似乎是这个问题所基于的链接问题的目标
$unique
将只保存
$array
中的每个
名称
属性作为键,循环后,任何具有重复名称的对象都将被删除。啊,不用担心。