note 69235 added to ref.array
| From: | ob at babcom dot biz | Date: | Mon, 28 Aug 2006 11:18:58 +0000 |
| Subject: | note 69235 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116424@lists.php.net to get a copy of this message | ||
Regarding the function of spam at madhermit dot net from January 9th 2006:
That function only preserves the deepest keys and values.
If you try to flatten an array with that function where the deepest instance of keys might be the
same where as keys in the "key-path" are different, values will be overwritten.
So here is a function that preserves the whole key-path and the keys of the flattened array will be
string keys consisting of the key-path separated by $Separator.
<?php
// flatten multidimensional array to one dimension
// preserves keys by generating a key for the flattened array which consists of the
// key-path of the multidimensional array separated by $Separator
// usage: array ArrayFlatten( array Array [, string Separator] )
function ArrayFlatten($Array,$Separator="_",$FlattenedKey='') {
$FlattenedArray=Array();
foreach($Array as $Key => $Value) {
if(is_Array($Value))
$FlattenedArray=Array_merge($FlattenedArray,
ArrayFlatten($Value,$Separator,
(strlen($FlattenedKey)>0
?$FlattenedKey.$Separator
:"").$Key)
);
else
$FlattenedArray[$FlattenedKey.$Separator.$Key]=$Value;
}
return $FlattenedArray;
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 84.185.247.44
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/69235
Del: integrated -- https://master.php.net/note/delete/69235/integrated
Del: useless -- https://master.php.net/note/delete/69235/useless
Del: bad code -- https://master.php.net/note/delete/69235/bad+code
Del: spam -- https://master.php.net/note/delete/69235/spam
Del: non-english -- https://master.php.net/note/delete/69235/non-english
Del: in docs -- https://master.php.net/note/delete/69235/in+docs
Del: other reasons-- https://master.php.net/note/delete/69235
Reject -- https://master.php.net/note/reject/69235
Search -- https://master.php.net/manage/user-notes.php