3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $sql = array(array('id' => 0, 'nom' => 'A', 'temp' => 1.1, 'autre' => 0), array('id' => 1, 'nom' => 'A', 'temp' => 2.1, 'autre' => 10), array('id' => 2, 'nom' => 'A', 'temp' => 3.1, 'autre' => 20), array('id' => 3, 'nom' => 'B', 'temp' => 4.1, 'autre' => 30), array('id' => 4, 'nom' => 'C', 'temp' => 5.1, 'autre' => 40), array('id' => 5, 'nom' => 'B', 'temp' => 6.1, 'autre' => 50), array('id' => 6, 'nom' => 'A', 'temp' => 7.1, 'autre' => 60)); $final = array(); foreach($sql as $s) { if(!in_array($s['nom'], array_values($final))) { array_push($final, $s['nom']); $i = array_search($s['nom'], $final); if(!isset($final[$i]['data'])) $final[$i]['data'] = array(); array_push($final[$i]['data'], $s['temp']); if(!isset($final[$i]['name'])) $final[$i]['name'] = $s['id']; } else { $i = array_search($s['nom'], $final); if(!isset($final[$i]['data'])) $final[$i]['data'] = array(); array_push($final[$i]['data'], $s['temp']); } } print_r($final); /* foreach ($sql as $s) if(in_array($final, $s['nom'])) { if(!isset($final[$s['nom']]['data'])) $final[$s['nom']]['data'] = array(); array_push($final[$s['nom']]['data'], $s['temp']); } else { array_push($final, $s['nom']); if(!isset($final[$s['nom']]['data'])) $final[$s['nom']]['data'] = array(); if(!isset($final[$s['nom']]['nom'])) $final[$s['nom']]['nom'] = $s['id']; array_push($final[$s['nom']]['data'], $s['temp']); } print_r($final); */
Output for 7.0.0 - 7.1.0
Warning: Illegal string offset 'data' in /in/jvC2f on line 17 Notice: Array to string conversion in /in/jvC2f on line 17 Warning: Illegal string offset 'data' in /in/jvC2f on line 18 Fatal error: Uncaught Error: Only variables can be passed by reference in /in/jvC2f:18 Stack trace: #0 {main} thrown in /in/jvC2f on line 18
Process exited with code 255.
Output for 5.4.0 - 5.6.28
Warning: Illegal string offset 'data' in /in/jvC2f on line 17 Notice: Array to string conversion in /in/jvC2f on line 17 Warning: Illegal string offset 'data' in /in/jvC2f on line 18 Fatal error: Only variables can be passed by reference in /in/jvC2f on line 18
Process exited with code 255.
Output for 4.3.2 - 4.4.9, 5.0.3 - 5.3.29
Fatal error: Only variables can be passed by reference in /in/jvC2f on line 18
Process exited with code 255.
Output for 5.0.0 - 5.0.2
Notice: Array to string conversion in /in/jvC2f on line 17 Fatal error: Only variables can be passed by reference in /in/jvC2f on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Only variables can be passed by reference in /in/jvC2f on line 18