mramine364

flattening array algo

Jan 25th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.38 KB | None | 0 0
  1. <?php
  2.  
  3. $array = [
  4.     ['a', 'b'],
  5.     ['c', ['d','e']],
  6.     'f',
  7.     ['g']
  8. ];
  9.  
  10. function flattening_array(array $items, array &$flattened = [])
  11. {
  12.     foreach ($items as $item) {
  13.         if( is_array($item) )
  14.         {
  15.             flattening_array($item, $flattened);
  16.             continue;
  17.         }
  18.         $flattened[] = $item;
  19.     }
  20. }
  21.  
  22. $flat_array = [];
  23. flattening_array($array,$flat_array);
  24.  
  25. print_r( $flat_array );
  26.  
  27. ?>
Advertisement
Add Comment
Please, Sign In to add comment