Monday, November 5, 2012

php multidimensional array to single dimension array

<?php 

php convert multidimensional array to single dimension array

convert multidimensional array to single array php



function multidimensional_array_to_single_dimension_array($array) { 
   if (!
$array) return false;
   
$flat = array();
   
$iterator  = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
  
   foreach (
$iterator as $value$singhal[] = $value;
   return 
$singhal;
}
$arr=array(1,2,array(4,5),6,array(7,8,array(9,10,11,array(12,13,14))));

 
 
print_r(multidimensional_array_to_single_dimension_array($arr)); ?>



OUTPUT:

Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 [9] => 11 [10] => 12 [11] => 13 [12] => 14 )


2 comments:

  1. Try this :

    http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php/14972714#14972714

    http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array/14972389#14972389

    ReplyDelete

If you Satisfied , Please Leave a comment