Is there a function to reindex a multidimensional array?

2

Well, I need a help, I have a function that returns the following array:

Array (
    [0] => Array (
        [0] => valor1 
        [2] => valor2 
    )
    [1] => Array (
        [0] => valor3
        [1] => valor4 
        [3] => valor5
    )
) 

I need to turn it around:

Array (
    [0] => Array (
        [0] => valor1
        [1] => valor2
    )
    [1] => Array (
        [0] => valor3
        [1] => valor4
        [2] => valor5
    )
)

I saw in the NA stack that a guy solved the problem using this function:

var_dump(
    array_map(
        create_function('$x','$k = key($x);
            return (is_numeric($k)) ? array_values($x) : $x;'
        ),$aDiff
   )
);

Where Found

But I did not quite understand where to replace the values ...

Any help will be welcome!

    
asked by anonymous 27.11.2014 / 12:35

2 answers

4

Its difficulty, as of many, is to understand the hairy syntax of create_function () . Fortunately the creation of anonymous functions has been drastically improved with Closures , but that's another story.

To reindex an array, just pass it through array_values () as shown by Antony but, if you compare the implementation that you encountered with his will see that it is not necessary to iterate.

If the goal is to map the input array of one thing (messy indexes) to another (organized), just use array_map () and solve everything with half a line:

$array = array_map( 'array_values', $array );
    
27.11.2014 / 12:55
0

Use the array_values function:

for($i=0; $i<count($array); $i++){
    $array[$i] = array_values($array[$i]);  
}

Here has a test of it working.

    
27.11.2014 / 12:45