Flatten an array in PHP


/ Published in: PHP

This will outpu a numeric result array


Copy this code and paste it in your HTML
  1. function array_flatten($array, $flat = false)
  2. {
  3. if (!is_array($array) || empty($array)) return '';
  4. if (empty($flat)) $flat = array();
  5.  
  6. foreach ($array as $key => $val) {
  7. if (is_array($val)) $flat = array_flatten($val, $flat);
  8. else $flat[] = $val;
  9. }
  10.  
  11. return $flat;
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.