note 116643 added to function.array-walk-recursive
| From: | php dot net at askmarcos dot com | Date: | Tue, 03 Feb 2015 17:03:44 +0000 |
| Subject: | note 116643 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201584@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 72.52.91.14
Probable Submitter: 77.253.211.255
----
Manual Page -- http://php.net/manual/en/function.array-walk-recursive.php
Edit -- https://master.php.net/note/edit/116643
Del: integrated -- https://master.php.net/note/delete/116643/integrated
Del: useless -- https://master.php.net/note/delete/116643/useless
Del: bad code -- https://master.php.net/note/delete/116643/bad+code
Del: spam -- https://master.php.net/note/delete/116643/spam
Del: non-english -- https://master.php.net/note/delete/116643/non-english
Del: in docs -- https://master.php.net/note/delete/116643/in+docs
Del: other reasons-- https://master.php.net/note/delete/116643
Reject -- https://master.php.net/note/reject/116643
Search -- https://master.php.net/manage/user-notes.php