PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $data = [ (object) ['id' => 1, 'egg' => 0, 'typeProduct' => 'Яица'], (object) ['id' => 2, 'milk' => 10, 'typeProduct' => 'Молоко'], (object) ['id' => 3, 'egg' => 0, 'typeProduct' => 'Яица'], (object) ['id' => 4, 'milk' => 9, 'typeProduct' => 'Молоко'], ]; $result = array_reduce( $data, function($acc, $el) { $el = (array)$el; if (!isset($acc[$el['typeProduct']])) $acc[$el['typeProduct']] = 0; $acc[$el['typeProduct']] += $el[array_keys($el)[1]]; return $acc; }, [] ); print_r($result);
Show:  
Copy Clear