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(array_search($s['nom'], $final) == NULL) array_push($final, $s['nom']); } 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 5.3.0 - 5.6.28, 7.0.0 - 7.1.0
Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A ) Warning: in_array() expects parameter 2 to be array, string given in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be array, string given in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be array, string given in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be array, string given in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be array, string given in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be array, string given in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be array, string given in /in/Hne4p on line 19 Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A [6] => A [A] => Array ( [data] => Array ( [0] => 1.1 [1] => 2.1 [2] => 3.1 [3] => 7.1 ) [nom] => 0 ) [7] => A [8] => A [9] => B [B] => Array ( [data] => Array ( [0] => 4.1 [1] => 6.1 ) [nom] => 3 ) [10] => C [C] => Array ( [data] => Array ( [0] => 5.1 ) [nom] => 4 ) [11] => B [12] => A )
Output for hhvm-3.10.0 - 3.12.0
Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A ) Warning: in_array() expects parameter 2 to be an array or collection in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be an array or collection in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be an array or collection in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be an array or collection in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be an array or collection in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be an array or collection in /in/Hne4p on line 19 Warning: in_array() expects parameter 2 to be an array or collection in /in/Hne4p on line 19 Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A [6] => A [A] => Array ( [data] => Array ( [0] => 1.1 [1] => 2.1 [2] => 3.1 [3] => 7.1 ) [nom] => 0 ) [7] => A [8] => A [9] => B [B] => Array ( [data] => Array ( [0] => 4.1 [1] => 6.1 ) [nom] => 3 ) [10] => C [C] => Array ( [data] => Array ( [0] => 5.1 ) [nom] => 4 ) [11] => B [12] => A )
Output for 4.3.2 - 5.2.17
Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A ) Warning: in_array(): Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array(): Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array(): Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array(): Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array(): Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array(): Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array(): Wrong datatype for second argument in /in/Hne4p on line 19 Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A [6] => A [A] => Array ( [data] => Array ( [0] => 1.1 [1] => 2.1 [2] => 3.1 [3] => 7.1 ) [nom] => 0 ) [7] => A [8] => A [9] => B [B] => Array ( [data] => Array ( [0] => 4.1 [1] => 6.1 ) [nom] => 3 ) [10] => C [C] => Array ( [data] => Array ( [0] => 5.1 ) [nom] => 4 ) [11] => B [12] => A )
Output for 4.3.0 - 4.3.1
Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A ) Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/Hne4p on line 19 Warning: in_array() [http://www.php.net/function.in-array]: Wrong datatype for second argument in /in/Hne4p on line 19 Array ( [0] => A [1] => A [2] => A [3] => B [4] => C [5] => A [6] => A [A] => Array ( [data] => Array ( [0] => 1.1 [1] => 2.1 [2] => 3.1 [3] => 7.1 ) [nom] => 0 ) [7] => A [8] => A [9] => B [B] => Array ( [data] => Array ( [0] => 4.1 [1] => 6.1 ) [nom] => 3 ) [10] => C [C] => Array ( [data] => Array ( [0] => 5.1 ) [nom] => 4 ) [11] => B [12] => A )