PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $arr = [ ["apple", "10", "10", "10"], ["apple", "20", "5", "10"], ["apple", "15", "5", "10"], ["banana", "10", "10", "10"], ["orange", "10", "15", "20"], ["orange", "5", "15", "10"], ]; $grouped = array_map( function ($n) { for ($i = 1; $i < count($n); $i++) { $min = min($n[$i]); $max = max($n[$i]); $n[$i] = $min === $max ? $min : "$min-$max"; } return $n; }, array_values( array_reduce( $arr, function ($acc, $n) { $acc[$n[0]] ??= [$n[0]]; for ($i = 1; $i < count($n); $i++) { $acc[$n[0]][$i][] = $n[$i]; } return $acc; }, [] ) ) ); print_r($grouped);
Show:  
Copy Clear