PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $input = [ [ "receipt_id" => 1, "phone" => "7777777777", "amount" => "5682", "status" => "Success", "item_id" => 1, "quantity" => 23, "price" => 5682 ], [ "receipt_id" => 1, "phone" => "7777777777", "amount" => "5682", "status" => "Success", "item_id" => 1, "quantity" => 23, "price" => 5682 ], ]; $output = array_reduce( $input, function($acc, $r) { if (!isset($acc[$r["receipt_id"]])) { $acc[$r["receipt_id"]] = [ "receipt_id" => $r["receipt_id"], "phone" => $r["phone"], "amount" => $r["amount"], "status" => $r["status"], "receipt_item" => [] ]; } $acc[$r["receipt_id"]]["receipt_item"][] = [ "item_id" => $r["item_id"], "quantity" => $r["quantity"], "price" => $r["price"] ]; return $acc; }, [] ); print_r($output);
Show:  
Copy Clear