Re: Proposal: array_flatten function
| From: | Mike Panchenko | Date: | Fri, 26 Dec 2008 18:28:28 +0000 |
| Subject: | Re: Proposal: array_flatten function | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-42384@lists.php.net to get a copy of this message | ||
Lukas,
Hmmm I'm just trying to solve the basic problem (hence my skepticism towards
preserving keys - it seems an edge case). I think once you get to more
specific requirements, iterators are probably the way to go, since all the
standard functions you could use to shorten this would re-iterate over the
array internally anyway.
Cheers.
Mike.
On Fri, Dec 26, 2008 at 7:56 AM, Lukas Kahwe Smith <mls@pooteeweet.org>wrote:
> In one of my recent projects I had the need for something like this. I
> essentially had a recursive data structure of organizations and frequently I
> had to get a list of all organization ID's that were above or underneath a
> given organization. So I had to write a deeply self join which would produce
> lets say around 3-4 columns that either contained an ID or were NULL.
>
> I ended up with an implementation like the following. Actually looking at
> it now it seems way too complex but it works, so it goes:
> function flattenArray($array) {
> if (empty($array)) {
> return array();
> }
> $first = reset($array);
> if (empty($first)) {
> return array();
> }
> $keys = array_keys($first);
> $result = array();
> foreach ($array as $row) {
> foreach ($keys as $key) {
> if (!empty($row[$key])) {
> $result[] = $row[$key];
> }
> }
> }
> $result = array_unique($result);
> return $result;
> }
>
> So I guess what I am asking for is a flag to ignore NULL's. Kind of reminds
> me of that patch Igor proposed the other day. Then again, we have sooooo
> many array functions its not even funny. So maybe we really should look more
> towards Iterators to help us consolidate things. Not sure.
>
> regards,
> Lukas
>