-
-
Save Danack/e60e18c76ae5216618760fedaf8389d3 to your computer and use it in GitHub Desktop.
array_mangling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = [ | |
'a' => 1, | |
'b' => 2, | |
'c' => 3, | |
'd' => 4, | |
]; | |
function mangle_array($key_to_remove, $key_to_place_after, array $data) | |
{ | |
$tmp_value = $data[$key_to_remove]; | |
$new_data = $data; | |
unset($new_data[$key_to_remove]); | |
$to_place_after_position = array_search($key_to_place_after, array_keys($new_data)); | |
$before = array_slice($new_data, 0, $to_place_after_position + 1); | |
$middle = [$key_to_remove => $tmp_value]; | |
$after = array_slice($new_data, $to_place_after_position + 1); | |
return array_merge($before, $middle, $after); | |
} | |
$data_mangled = mangle_array('b', 'c', $data); | |
var_dump($data_mangled); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment