note 60518 deleted from ref.array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 19:23:26 +0000 |
| Subject: | note 60518 deleted from ref.array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134501@lists.php.net to get a copy of this message | ||
Note Submitter: spam at madhermit dot net
----
Here is a function that recursively flattens an multidimensional array while maintaining keys.
Hopefully it is useful to someone..
Example Input:
Array
(
[name] => John Doe
[email] => johndoe@earthlink.net
[addresses] => Array
(
[1] => Array
(
[address] => 555 Somewhere
[city] => Podunk
[state] => CA
[zip] => 90120
)
[2] => Array
(
[address] => 333 Someother Place
[city] => Podunk
[state] => CA
[zip] => 91103
)
)
)
Example output:
Array
(
[name] => John Doe
[email] => johndoe@earthlink.net
[address1] => 555 Somewhere
[city1] => Podunk
[state1] => CA
[zip1] => 90120
[address2] => 333 Someother Place
[city2] => Podunk
[state2] => CA
[zip2] => 91103
)
<?
function flattenArray($array,$keyname='')
{
$tmp = array();
foreach($array as $key => $value)
{
if(is_array($value))
$tmp = array_merge($tmp,flattenArray($value,$key));
else
$tmp[$key.$keyname] = $value;
}
return $tmp;
}
?>