3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $user = []; $arr = [ 'chatduration' => '124.502','username' => 'bhavin']; foreach($arr as $record){ if (array_key_exists($user[$record['username']],$record)) { $user[$record['username']] = $user[$record['username']] + $record['chatduration']; } else { $user[] = [$record['username']=>$record['chatduration']]; } } print_r($arr);
based on irdVW
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.3.0rc3
Warning: Illegal string offset 'username' in /in/sYPbA on line 7 Notice: Undefined offset: 1 in /in/sYPbA on line 7 Warning: array_key_exists() expects parameter 2 to be array, string given in /in/sYPbA on line 7 Warning: Illegal string offset 'username' in /in/sYPbA on line 13 Warning: Illegal string offset 'chatduration' in /in/sYPbA on line 13 Warning: Illegal string offset 'username' in /in/sYPbA on line 7 Notice: Undefined index: b in /in/sYPbA on line 7 Warning: array_key_exists() expects parameter 2 to be array, string given in /in/sYPbA on line 7 Warning: Illegal string offset 'username' in /in/sYPbA on line 13 Warning: Illegal string offset 'chatduration' in /in/sYPbA on line 13 Array ( [chatduration] => 124.502 [username] => bhavin )
Output for hhvm-3.22.0
Notice: Undefined index: 1 in /in/sYPbA on line -1 Warning: Invalid operand type was used: array_key_exists expects an array or an object; false returned. in /in/sYPbA on line -1 Notice: Undefined index: b in /in/sYPbA on line -1 Warning: Invalid operand type was used: array_key_exists expects an array or an object; false returned. in /in/sYPbA on line -1 Array ( [chatduration] => 124.502 [username] => bhavin )
Output for hhvm-3.18.5 - 3.21.3
Notice: Undefined index: 1 in /in/sYPbA on line 7 Warning: Invalid operand type was used: array_key_exists expects an array or an object; false returned. in /in/sYPbA on line 7 Notice: Undefined index: b in /in/sYPbA on line 7 Warning: Invalid operand type was used: array_key_exists expects an array or an object; false returned. in /in/sYPbA on line 7 Array ( [chatduration] => 124.502 [username] => bhavin )