3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // Simple array merging $array1 = [10, 50, 20, 40, 30]; $array2 = [1.0, 5.0, 2.0, 4.0, 3.0]; print_r(array_merge($array1, $array2)); // Sorting table-like arrays that has sorted by key elements $table1 = [ '2011:london' => ['city' => 'London', 'year' => 2011, 'birthrate' => 'over9000'], '2012:birmingham' => ['city' => 'Birmingham', 'year' => 2012, 'birthrate' => 'wat'], '2013:london' => ['city' => 'London', 'year' => 2013, 'birthrate' => 'boo boo'] ]; $table2 = [ '2011:london' => ['city' => 'London', 'year' => 2011, 'comment' => 1], '2012:london' => ['city' => 'London', 'year' => 2012, 'comment' => 22], '2014:london' => ['city' => 'London', 'year' => 2014, 'comment' => 333] ]; print_r(array_linear_merge($table1, $table2)); print_r(array_linear_merge2($table1, $table2)); // New func for merging sorted by unique key table-like arrays! // Complexity O(m+n), where m and n are sizes of each array. function array_linear_merge($array1, $array2) { $iterator1 = new ArrayIterator($array1); $iterator2 = new ArrayIterator($array2); $result = []; while ($iterator1->valid() && $iterator2->valid()) { switch(sign(strcmp($iterator1->key(), $iterator2->key()))) { case 1: $result[] = $iterator2->current(); $iterator2->next(); break; case 0: $result[] = array_merge($iterator1->current(), $iterator2->current()); $iterator1->next(); $iterator2->next(); break; case -1: $result[] = $iterator1->current(); $iterator1->next(); break; } } if (!$iterator1->valid()) { $iterator1 = $iterator2; } while ($iterator1->valid()) { $result[] = $iterator1->current(); $iterator1->next(); } return $result; } function array_linear_merge($array1, $array2) { reset($array1); reset($array2); $result = []; while (current($array1) !== false && current($array2) !== false) { switch(sign(strcmp(key($array1), key($array2)))) { case 1: $result[] = current($array2); next($array2); break; case 0: $result[] = array_merge(current($array1), current($array2)); next($array1); next($array2); break; case -1: $result[] = current($array1); next($array1); break; } } while ($next = next($array1)) { $result[] = $next; } while ($next = next($array2)) { $result[] = $next; } return $result; } function sign($number) { return ($number > 0) - ($number < 0); }
based on 2YPOG
Output for 5.4.0 - 5.4.32
Fatal error: Cannot redeclare array_linear_merge() (previously declared in /in/VrjOh:24) in /in/VrjOh on line 77
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/VrjOh on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/VrjOh on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/VrjOh on line 4
Process exited with code 255.