note 89450 added to function.array-values
| From: | id_ivan at yahoo dot com | Date: | Sun, 08 Mar 2009 11:36:42 +0000 |
| Subject: | note 89450 added to function.array-values | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151626@lists.php.net to get a copy of this message | ||
i missed one array value for mistakenly putting the recursive function outside the loop. so here is
the corrected function:
<?php
/* ---------------------
* @function array_flatten
* @param array
* @since 0.1
* @return array
* @notes flatten associative multi dimension array recursive
* @update 18:31 3/8/2009
* @author Rivanoor Bren <id_ivan(at)yahoo.com>
---------------------- */
function array_flatten($array, $preserve = FALSE, $r = array()){
foreach($array as $key => $value){
if (is_array($value)){
foreach($value as $k => $v){
if (is_array($v)) { $tmp = $v; unset($value[$k]); }
}
if ($preserve) $r[$key] = $value;
else $r[] = $value;
}
// this is correct
$r = isset($tmp) ? array_flatten($tmp, $preserve, $r) : $r;
}
// wrong spot:
// $r = isset($tmp) ? array_flatten($tmp, $preserve, $r) : $r;
return $r;
}
?>
----
Server IP: 202.158.52.162
Probable Submitter: 125.160.97.45
----
Manual Page -- http://www.php.net/manual/en/function.array-values.php
Edit -- https://master.php.net/note/edit/89450
Del: integrated -- https://master.php.net/note/delete/89450/integrated
Del: useless -- https://master.php.net/note/delete/89450/useless
Del: bad code -- https://master.php.net/note/delete/89450/bad+code
Del: spam -- https://master.php.net/note/delete/89450/spam
Del: non-english -- https://master.php.net/note/delete/89450/non-english
Del: in docs -- https://master.php.net/note/delete/89450/in+docs
Del: other reasons-- https://master.php.net/note/delete/89450
Reject -- https://master.php.net/note/reject/89450
Search -- https://master.php.net/manage/user-notes.php