Created
November 4, 2011 17:28
-
-
Save justinwalsh/1339930 to your computer and use it in GitHub Desktop.
PHP function to rekey or index a multidimensional array and preserve associative keys
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 | |
function rekey_multi_array($array) { | |
$new = array(); | |
$count = 0; | |
if (is_array($array)) { | |
foreach ($array as $key => $val) { | |
if (!is_numeric($key)) { | |
$new[$key] = $this -> rekey_multi_array($val); | |
} else { | |
$new[$count] = $this -> rekey_multi_array($val); | |
$count++; | |
} | |
} | |
return $new; | |
} else { | |
return $array; | |
} | |
} | |
?> |
Works great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you SOOO much. Been looking for just this function!