3v4l.org

run code in 300+ PHP versions simultaneously
<?php $s1 = '{ "b": "b", "2": "2", "3": "a", "4": "4", "6": "5", "7": "6" }'; $s2 = '{ "21": "b", "2": "2", "3": "3", "4": "4", "6": "5", "7": "6" }'; $e1 = json_decode($s1,true); $e2 = json_decode($s2,true); function custom_splice(&$ar, $a, $b){ $out = array_splice($ar, $a, 1); array_splice($ar, $b, 0, $out); } function moveElement(&$array, $a, $b) { $keys = array_keys($array); custom_splice($array, $a, $b); custom_splice($keys, $a, $b); $array = array_combine($keys,$array); } moveElement($e1, 2, 0); moveElement($e2, 2, 0); print_r($e1); print_r($e2);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Array ( [3] => a [b] => b [2] => 2 [4] => 4 [6] => 5 [7] => 6 ) Array ( [3] => 3 [21] => b [2] => 2 [4] => 4 [6] => 5 [7] => 6 )

preferences:
136.64 ms | 407 KiB | 5 Q