简体   繁体   English

数组中对象属性的不同值?

[英]Distinct values from an object property within an array?

I'm making an array with an object inside of it and I need to check when looping and adding whether the object property name already has a duplicate value and then add only distinct results to the array.我正在创建一个包含一个对象的数组,我需要在循环和添加对象属性名称时检查它是否已经具有重复值,然后只将不同的结果添加到数组中。

$lifestyle = array();    
foreach ($this->images as $image) {
    if(!in_array($image->getName(), $lifestyle['name'])){
       $lifestyle[] = $image;
    }
}

But the $lifestyle['name'] on line 3, obviously isn't targeting the object, but I'm not really sure how to do this!但是第 3 行的$lifestyle['name']显然不是针对对象的,但我不确定如何做到这一点! I just keep running into one error or another.我只是不断遇到一个或另一个错误。

The array/object that is made when I don't try and filter out duplicates looks like this:当我不尝试过滤重复项时创建的数组/对象如下所示:

array (size=3)
0 => 
object(Pingu\ProductBundle\Entity\Image)[746]
  private 'id' => int 1175
  private 'name' => string 'cl0021_02' (length=9)
  private 'main' => boolean false
  private 'active' => boolean true
  private 'type' => string 'lifestyle' (length=9)
1 => 
object(Pingu\ProductBundle\Entity\Image)[747]
  private 'id' => int 1176
  private 'name' => string 'cl0021_02' (length=9)
  private 'main' => boolean false
  private 'active' => boolean true
  private 'type' => string 'lifestyle' (length=9)
2 => 
object(Pingu\ProductBundle\Entity\Image)[748]
  private 'id' => int 1177
  private 'name' => string 'cl0021_02' (length=9)
  private 'main' => boolean false
  private 'active' => boolean true
  private 'type' => string 'lifestyle' (length=9)

Your code is in fact wrong around the $lifestyle['name'] part, as you pointed out.正如您所指出的,您的代码实际上在$lifestyle['name']部分是错误的。

in_array function expects an array as second argument, but you try to set it with $lifestyle['name'] , which is not correct (and doesn't exist). in_array函数需要一个数组作为第二个参数,但您尝试使用$lifestyle['name']设置它,这是不正确的(并且不存在)。

Your $lifestyle var is an indexed array, in which you add the images (line 4).您的$lifestyle var 是一个索引数组,您可以在其中添加图像(第 4 行)。

If you want to check whether a given property value exists in the array of images objects, you can't do that with in_array ( in_array is used with statements like that : in_array(10, [1,2,3]); // evaluates to FALSE .如果你想检查图像对象数组中是否存在给定的属性值,你不能用in_arrayin_array与这样的语句一起使用: in_array(10, [1,2,3]); // evaluates to FALSE

You have to loop through the entire $lifestyle array and check for each item it's name property :您必须遍历整个$lifestyle数组并检查它的 name 属性中的每个项目:

$lifestyle = array();    
foreach ($this->images as $image)
{
    $found = false;
    foreach ( $lifestyle as $item )    
        if ( $item->name == $image->getName() )
        {
            $found = true;
            break; // don't bother to continue $lifestyle loop, item is duplicate
        }

    // check if previous loop found something
    if ( !$found )
        $lifestyle[] = $image;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM