[ create a new paste ] login | about

Link: http://codepad.org/IWtILiBD    [ raw code | output | fork ]

PHP, pasted on Feb 23:
<?php


function flattenArray($array) {

   $flattened = array();

   foreach($array as $value) {

       if (is_array($value)) {
           $flattened = array_merge($flattened, flattenArray($value));

       } else {

           $flattened[] = $value;
       }

   }

   return $flattened;

}

var_dump(flattenArray(array(1, array(2, 3), array(4, array(5, 6)))));


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}


Create a new paste based on this one


Comments: