3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $arr = array('a','b','c','d','e','f','g','h','i','j','k','l'); $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] => a [amount] => 1 ) [1] => Array ( [value] => b [amount] => 1 ) [2] => Array ( [value] => c [amount] => 1 ) [3] => Array ( [value] => d [amount] => 1 ) [4] => Array ( [value] => e [amount] => 1 ) [5] => Array ( [value] => f [amount] => 1 ) [6] => Array ( [value] => g [amount] => 1 ) [7] => Array ( [value] => h [amount] => 1 ) [8] => Array ( [value] => i [amount] => 1 ) [9] => Array ( [value] => j [amount] => 1 ) [10] => Array ( [value] => k [amount] => 1 ) [11] => Array ( [value] => l [amount] => 1 ) )