note 87757 modified in ref.array by danbrown

From: Date: Fri, 19 Dec 2008 19:26:34 +0000
Subject: note 87757 modified in ref.array by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148364@lists.php.net to get a copy of this message
Recursive multidimensional array flatten using SPL <?php function array_flatten_recursive($array) { if($array) { $flat = array(); foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::SELF_FIRST) as $key=>$value) { if(!is_array($value)) { $flat[] = $value; } } return $flat; } else { return false; } } $array = array( 'A' => array('B' => array( 1, 2, 3, 4, 5)), 'C' => array( 6,7,8,9) ); print_r(array_flatten_recursive($array)); ?> -- Returns: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) --was-- Recursive multidimensional array flatten using SPL function array_flatten_recursive($array) { if($array) { $flat = array(); foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::SELF_FIRST) as $key=>$value) { if(!is_array($value)) { $flat[] = $value; } } return $flat; } else { return false; } } $array = array( 'A' => array('B' => array( 1, 2, 3, 4, 5)), 'C' => array( 6,7,8,9) ); print_r(array_flatten_recursive($array)); -- Returns: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) http://php.net/manual/en/ref.array.php

« previous php.notes (#148364) next »