PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Hint: use Ctrl+Enter for SQL autocomplete CREATE TABLE `list` ( id varchar(32), count int ); INSERT INTO `list` VALUES ("1-2", 1), ("1-3", 12);
Copy Clear
Copy Format Clear
<?php $data = [ [ "id_product_attribute" => "60" , "attribute_name" => "S" ], [ "id_product_attribute" => "59" , "attribute_name" => "M" ], [ "id_product_attribute" => "57" , "attribute_name" => "M" ], [ "id_product_attribute" => "56" , "attribute_name" => "XL" ], [ "id_product_attribute" => "58" , "attribute_name" => "XL" ], [ "id_product_attribute" => "56" , "attribute_name" => "Grey" ], [ "id_product_attribute" => "59" , "attribute_name" => "Grey" ], [ "id_product_attribute" => "57" , "attribute_name" => "Grey" ], [ "id_product_attribute" => "58" , "attribute_name" => "Grey" ], [ "id_product_attribute" => "60" , "attribute_name" => "Yellow" ], [ "id_product_attribute" => "56" , "attribute_name" => "40x60cm" ], [ "id_product_attribute" => "57" , "attribute_name" => "40x60cm" ], [ "id_product_attribute" => "58" , "attribute_name" => "60x90cm" ], [ "id_product_attribute" => "59" , "attribute_name" => "60x90cm" ], ]; $result = array_reduce( $data, function($res, $el) { if (isset($res[$el["id_product_attribute"]])) { $res[$el["id_product_attribute"]] .= ", " . $el["attribute_name"]; } else { $res[$el["id_product_attribute"]] = $el["attribute_name"]; } return $res; }, [] ); var_export($result);
Show:  
Copy Clear