Flattening an array in PHP

php

You might have a multidimensional array in PHP that you might need to flatten to make it easier to work with. Let’s take the example “myArray”.

As you see from the code block below, you can create a temporary object and then from there you use array_walk_recursive to flatten the array into an easy to use format.


$objTmp = (object) array('aFlat' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);

Leave a Reply

Your email address will not be published. Required fields are marked *