I have an associative array that is many levels deep. I want to convert it to an indexed array (i.e. remove the key names). I don't want to flatten it; I want it to remain a nested array with all of the values intact. What's the best way to accomplish this?

5 Years
Discussion Span
Last Post by Dani

I quickly put together this recursion function.

function assoc2indexedMulti($arr) {

    // initialize destination indexed array
    $indArr = array();

    // loop through source
    foreach($arr as $val) {

        // if the element is array call the recursion
        if(is_array($val)) {

            $indArr[] = assoc2indexedMulti($val);

        // else add the value to destination array
        } else {

            $indArr[] = $val;

    return $indArr;

Tested it on a small array, I hope it works OK on your array. If the array is large it might be an issue since it doubles the memory requirement.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.