Porto

Flatten Array

Flatten Array

Ever needed to take a multi-dimensional array and flatten it to produce a single dimension array. This method using iterators is far less intensive than using recursion.


 <?php
 
/**
 *
 * @flatten multi-dimensional array
 *
 * @param array $array
 *
 * @return array
 *
 */
 
function flattenArray(array $array){
  
$ret_array = array();
  foreach(new 
RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value)
     {
     
$ret_array[] = $value;
     }
  return 
$ret_array;
  }

 
?>