note 116643 deleted from function.array-walk-recursive by salathe
| From: | salathe@php.net | Date: | Fri, 06 Feb 2015 13:39:09 +0000 |
| Subject: | note 116643 deleted from function.array-walk-recursive by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201646@lists.php.net to get a copy of this message | ||
Note Submitter: php dot net at askmarcos dot com
----
This how I use array_walk_recursive() to implement
array_flatten() that actually preserves keys, if they are provided inside the incoming parameter:
static public function array_flatten($inObj)
{
$outObj = [];
$inObj=[$inObj]; // Thanks to which nonArray param is also welcome here.
array_walk_recursive($inObj,
function ($incell, $inkey) use (&$outObj)
{
$outObj[$inkey] = $incell;
} );
return $outObj;
}
Note that you are responsible for ensuring that the keys in $inObj are globally unique (and either
string or int type), otherwise, I don't know the worst that can happen.
Probably later elements overwrite earlier ones having the same key name, rather than appending to
the final $outObj to return.