Skip to content

Instantly share code, notes, and snippets.

@justinwalsh
Created November 4, 2011 17:28
Show Gist options
  • Save justinwalsh/1339930 to your computer and use it in GitHub Desktop.
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
<?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;
}
}
?>
@gavin310
Copy link

Works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment