3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $arr = array(1,1,1,2,2,3,3,1,1,2,2,3); $result = array(); $prev_value = array('value' => null, 'amount' => null); foreach ($arr as $val) { if ($prev_value['value'] != $val) { unset($prev_value); $prev_value = array('value' => $val, 'amount' => 0); $result[] =& $prev_value; } $prev_value['amount']++; } print_r($result);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [0] => Array ( [value] => 1 [amount] => 3 ) [1] => Array ( [value] => 2 [amount] => 2 ) [2] => Array ( [value] => 3 [amount] => 2 ) [3] => Array ( [value] => 1 [amount] => 2 ) [4] => Array ( [value] => 2 [amount] => 2 ) [5] => Array ( [value] => 3 [amount] => 1 ) )