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 - 7.1.0
Notice: Use of undefined constant a - assumed 'a' in /in/1QfU5 on line 2 Notice: Use of undefined constant b - assumed 'b' in /in/1QfU5 on line 2 Notice: Use of undefined constant c - assumed 'c' in /in/1QfU5 on line 2 Notice: Use of undefined constant d - assumed 'd' in /in/1QfU5 on line 2 Notice: Use of undefined constant e - assumed 'e' in /in/1QfU5 on line 2 Notice: Use of undefined constant f - assumed 'f' in /in/1QfU5 on line 2 Notice: Use of undefined constant g - assumed 'g' in /in/1QfU5 on line 2 Notice: Use of undefined constant h - assumed 'h' in /in/1QfU5 on line 2 Notice: Use of undefined constant i - assumed 'i' in /in/1QfU5 on line 2 Notice: Use of undefined constant j - assumed 'j' in /in/1QfU5 on line 2 Notice: Use of undefined constant k - assumed 'k' in /in/1QfU5 on line 2 Notice: Use of undefined constant l - assumed 'l' in /in/1QfU5 on line 2 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 ) )