There is an array with data, you need to overwrite it in order to get an array with data for each product. But, you still need to write several identical properties in one line in the form of value 1, <br> value 2, <br> value3 and wrap it in <![CDATA[ . . . ]]> <![CDATA[ . . . ]]> <![CDATA[ . . . ]]> . I managed to write in this form only 2 values (second foreach ), how to do if there are more values?
$feature = array(); $result = [ {"feature_id"=>"171", "name"=>"param 1","product_id"=>5,"value"=>"value 1"}, {"feature_id"=>"171", "name"=>"param 1","product_id"=>5,"value"=>"value 2"}, {"feature_id"=>"295", "name"=>"param 2","product_id"=>5,"value"=>"value 555"}, {"feature_id"=>"171", "name"=>"param 1","product_id"=>5,"value"=>"value 3"} ]; foreach($result as $f) { if(!empty($f->name)) { $features[$f->product_id][$f->name] = $f->value; } } ////// foreach($result as $f) { if(!empty($f->name)) { if(is_array($features[$f->product_id]) && array_key_exists($f->name, $features[$f->product_id])) { $features[$f->product_id][$f->name] = "<![CDATA[" . $features[$f->product_id][$f->name] . ", <br> " . $f->value . "]]>"; } else { $features[$f->product_id][$f->name] = $f->value; } } }
implode- teran