0

What I want to achieve?

"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": []
            }
        ]

what I have tried: I fetch whole attributes from the DB and then compare it to product attribute and made this format now the problem is when I start traversing its comparing result from all attribute which creates an empty object every time my if() condition fails.

how can I remove empty object having empty records array and reindex my final array?

here is my code :

$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();
                    }

what i have tried:

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

TEST CASE:

  1. Fetching all attributes from the subcategories of which product belongs to.

  2. fetching all product attributes from product attribute table

  3. then comparing the title of all attributes with product attributes when found the same record I have put this in inside the array. so I achieve color=>red, black this type of structure instead of color=>red, color=>black

  4. now the test case is when all attributes have 4 attributes color, size, ram, processor, and product having only two color and ram at this case my loop give me empty record as with the last title I want to remove that object having an empty record.

thanks in advance :)

all attribute table product attribute table

**NEW TRY: **

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 


      }
  }
2
  • do you still want to use Query Builder? if you're open to suggestions, I think writing models are a better way Commented Nov 14, 2018 at 9:16
  • yes i can use that too if you have suggestion about that please share Commented Nov 14, 2018 at 9:37

2 Answers 2

1

Someone already posted using filter its definitely the solution for you. You don't need to have get the data from the database to use a collection.

collect($productAttributesArray).filter(function($product){
  return !empty($product->records);
}
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your reply Mike Miller I agree with u but here the problem is database doesn't have empty value
I have added my database table screenshot .
I dont follow. You mean the records array is not empty?
yes, its the combination I have checked for example a Samsung phone has attribute color and ram only and mobile subcategories have color ram processor size etc all so when I checked for the combination of the title (which is color, ram ) if match I put them in one object.
I think you are approaching this the wrong way. I don't really understand what you said but sounds like your data model is wrong. If you want to achieve the array structure shown at the top of your post you should model your DB schema like that OR use a NoSQL database
|
1

you could try to filter the collection for rows the doesn't have record.

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

$product_attributes is the same array you have foreached. what this do is convert the array to a collection and filter out all object with a non empty records.

4 Comments

the test is not from database it's like this am editing my question for showing my test case wait..and thanx for reply @Joe
i have updated my answer, using the same array that you echoed above, you need to convert it to a collection and filter them out
ok so make my final array as a collection then filtering records for the empty object. But what is $item here is can u explain code more so i can understand
the parameter $item is the single element of the collection, and it iterate and return only non-empty $item->records. you could name it anyway you want. it's like doing foreach($product_attributes as $item)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.