3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ //... 'name' => ['value' => 'Raj KB'], 'street' => ['value' => 'Street ABC'], 'city' => ['value' => 'Dubai'], 'country_id' => ['value' => 'UAE'], 'region' => ['value' => 'DXB'], 'region_id' => ['value' => 11], 'zip_code' => ['value' => 12345], 'city_id' => ['value' => 22], //... ]; $ordered_keys = ['country_id', 'region', 'region_id', 'city', 'city_id']; $flag = true; $result = []; foreach ($array as $k=>$v) { if ($flag && in_array($k, $ordered_keys)) { $flag = false; foreach ($ordered_keys as $key) { $result[$key] = $array[$key]; } } else { $result[$k] = $array[$k]; } } print_r($result);
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.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.14, 8.4.16 - 8.4.18, 8.5.0 - 8.5.3
Array ( [name] => Array ( [value] => Raj KB ) [street] => Array ( [value] => Street ABC ) [country_id] => Array ( [value] => UAE ) [region] => Array ( [value] => DXB ) [region_id] => Array ( [value] => 11 ) [city] => Array ( [value] => Dubai ) [city_id] => Array ( [value] => 22 ) [zip_code] => Array ( [value] => 12345 ) )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
197.82 ms | 408 KiB | 5 Q