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:
Fetching all attributes from the subcategories of which product belongs to.
fetching all product attributes from product attribute table
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
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 :)
**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
}
}

