3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['one' => 'data 1'], ['two' => 'data 2'], ['two' => 'data 3'], ['three' => 'data 4'], ['two' => 'data 5'], ['two' => 'data 6'], ['two' => 'data 7'], ['four' => 'data 8'], ]; $result = array_reduce($array, function ($c, $a) { $len = count($c); $key = key($a); if ($key == key($c[$len-1] ?? [])) { if (is_array($c[$len-1][$key])) { $c[$len-1][$key][]= $a[$key]; } else { $c[$len-1][$key] = array($c[$len-1][$key], $a[$key]); } } else { $c[] = $a; } return $c; }, []); print_r($result);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => Array ( [one] => data 1 ) [1] => Array ( [two] => Array ( [0] => data 2 [1] => data 3 ) ) [2] => Array ( [three] => data 4 ) [3] => Array ( [two] => Array ( [0] => data 5 [1] => data 6 [2] => data 7 ) ) [4] => Array ( [four] => data 8 ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
42.87 ms | 1627 KiB | 4 Q