【问题标题】:how can i remove whole object from the array if that has empty array inside?如果里面有空数组,我如何从数组中删除整个对象?
【发布时间】:2018-11-14 08:03:28
【问题描述】:

我想达到什么目的?

"product_attributes": [
            {
                "title": "Color",
                "records": [
                    {
                        "attribute_name": "black",
                        "mpr": "100"
                    },
                    {
                        "attribute_name": "green",
                        "mpr": "200"
                    }
                ]
            },
            {
                "title": "RAM",
                "records": [
                    {
                        "attribute_name": "16GB",
                        "mpr": "10"
                    }
                ]
            },
            {
                "title": "RAM",///remove this whole obeject 
                "records": []
            }
        ]

我尝试过的:我从数据库中获取整个属性,然后将其与产品属性进行比较并制作这种格式,现在问题是当我开始遍历所有属性的比较结果时,每次我的 if( ) 条件失败。

如何删除具有空记录数组的空对象并重新索引我的最终数组?

这是我的代码:

$allattributes = DB::table('product_attributes')->where('subcategory_id', $product->subcat_id)->get(['attribute_title']);

                    $valuesnew = DB::table('current_product_attribute_values')->where('product_id', $product->id)->get();

                        $emptybool=false;
                    // checking for empty attributes 

                    if ($valuesnew->count() > 0) {




                        // first foreach for 3 value 
                        foreach ($allattributes as $name) {

                            //echo $name->attribute_title;

                            $boo = false;


                            //   40 record loop 

                            $records = array();
                            foreach ($valuesnew as $compare) {


                                //   if attibute title are same
                                if ($compare->attribute_title == $name->attribute_title) {

                                    if ($boo == false) {

                                        $titledata = $name->attribute_title;

                                        $holddata['title'] = $titledata;


                                        $boo = true;
                                    }

                                    $records[] = array("attribute_name" => $compare->attribute_value, "mpr" => $compare->attribute_mrp);
                                }

                            }


                            $holddata['records'] = $records;
                            $final[] = $holddata;


                        }
                    } else {
                        $final = array();
                    }

我尝试过的:

foreach($final as $k=>$arr){ 

    //$final=array_filter($arr,'count');
    if(array_filter($arr,'count') || array_values(array_filter($arr))){
        unset($arr[$i]);
}

 $i++;   

}

print_r($final);//failed

测试用例:

  1. 从产品所属的子类别中获取所有属性。

  2. 从产品属性表中获取所有产品属性

  3. 然后在找到相同的记录时将所有属性的标题与产品属性进行比较,我已将其放入数组中。所以我实现了color=>red,black这种结构而不是color=>red,color=>black

  4. 现在测试用例是当所有属性都有 4 个属性颜色、大小、内存、处理器和产品只有两种颜色和内存在这种情况下,我的循环给了我空记录,就像我想要的最后一个标题一样删除具有空记录的对象。

提前感谢:)

**新尝试:**

foreach($final as $k=>$arr){ 


                    foreach($arr as $key=>$value){
      $count=count($value);
      if($count==0){
          echo '<pre>';
          echo ' am empty object remove me ';
          '<br>';
          unset($arr[$index]);//failed how can i remove this whole object from the main array 


      }
  }

【问题讨论】:

  • 您还想使用查询生成器吗?如果您愿意接受建议,我认为编写模型是一种更好的方式
  • 是的,如果您对此有任何建议,我也可以使用它,请分享

标签: php arrays laravel multidimensional-array


【解决方案1】:

有人已经使用filter 发布了它绝对是您的解决方案。您无需从数据库中获取数据即可使用集合。

collect($productAttributesArray).filter(function($product){
  return !empty($product->records);
}

【讨论】:

  • 感谢您的回复 Mike Miller 我同意您的观点,但这里的问题是数据库没有空值
  • 我已经添加了我的数据库表截图。
  • 我不关注。你的意思是记录数组不为空?
  • 是的,它是我检查过的组合,例如三星手机只有属性颜色和内存,而移动子类别有颜色内存处理器大小等,所以当我检查标题的组合时(即color, ram ) 如果匹配,我将它们放在一个对象中。
  • 我认为您以错误的方式处理此问题。我不太明白你在说什么,但听起来你的数据模型是错误的。如果您想实现文章顶部显示的数组结构,您应该像这样对您的数据库模式进行建模或使用 NoSQL 数据库
【解决方案2】:

您可以尝试filter 没有记录的行的集合。

$withRecordsOnly = collect($product_attributes)->filter(function ($item) {
    return !empty($item->records);
});

$product_attributes 与您拥有foreached 的数组相同。这样做是将数组转换为集合并过滤掉所有具有非空记录的对象。

【讨论】:

  • 测试不是来自数据库,就像正在编辑我的问题以显示我的测试用例等待..andthanx 回复@Joe
  • 我已经更新了我的答案,使用您在上面回显的相同数组,您需要将其转换为集合并将它们过滤掉
  • 好的,所以将我的最终数组作为一个集合,然后过滤空对象的记录。但是这里的 $item 是什么,你能解释一下代码以便我能理解吗
  • 参数$itemcollection的单个元素,它迭代并只返回非空$item-&gt;records。你可以随意命名它。这就像在做foreach($product_attributes as $item)
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 2016-11-26
  • 2020-02-01
  • 2015-04-04
  • 2015-08-29
  • 2016-02-26
  • 1970-01-01
相关资源
最近更新 更多