Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:42382 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55605 invoked from network); 26 Dec 2008 15:56:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Dec 2008 15:56:23 -0000 Authentication-Results: pb1.pair.com smtp.mail=mls@pooteeweet.org; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=mls@pooteeweet.org; sender-id=unknown Received-SPF: error (pb1.pair.com: domain pooteeweet.org from 88.198.8.16 cause and error) X-PHP-List-Original-Sender: mls@pooteeweet.org X-Host-Fingerprint: 88.198.8.16 bigtime.backendmedia.com Linux 2.6 Received: from [88.198.8.16] ([88.198.8.16:42180] helo=bigtime.backendmedia.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id ED/D8-33750-6AEF4594 for ; Fri, 26 Dec 2008 10:56:23 -0500 Received: from localhost (unknown [127.0.0.1]) by bigtime.backendmedia.com (Postfix) with ESMTP id 3B6441EBC013; Fri, 26 Dec 2008 15:56:35 +0000 (UTC) X-Virus-Scanned: amavisd-new at backendmedia.com Received: from bigtime.backendmedia.com ([127.0.0.1]) by localhost (bigtime.backendmedia.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ToQC+ZVPdOAd; Fri, 26 Dec 2008 16:56:33 +0100 (CET) Received: from [192.168.1.55] (f053010092.adsl.alicedsl.de [78.53.10.92]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: mls@pooteeweet.org) by bigtime.backendmedia.com (Postfix) with ESMTP id 62E3E414400F; Fri, 26 Dec 2008 16:56:33 +0100 (CET) Cc: internals@lists.php.net Message-ID: <03D6160A-376F-44A8-B82F-F31873CFA0BB@pooteeweet.org> To: Mike Panchenko In-Reply-To: <5faa7b820812260106p6f24bf0ct49ef574e5ed4e2c2@mail.gmail.com> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v930.3) Date: Fri, 26 Dec 2008 16:56:16 +0100 References: <5faa7b820812260106p6f24bf0ct49ef574e5ed4e2c2@mail.gmail.com> X-Mailer: Apple Mail (2.930.3) Subject: Re: [PHP-DEV] Proposal: array_flatten function From: mls@pooteeweet.org (Lukas Kahwe Smith) On 26.12.2008, at 10:06, Mike Panchenko wrote: > Hey everybody, I'm new both to the list and to hacking the > internals, so > I'll try to keep it short and humble. > > I've written an array_flatten function which just takes all elements > from a > nested array and pushes them into a single indexed array. > > Couple of questions: > > 1. Most importantly, is this patch-worthy to everybody? I was a bit > surprised that it didn't already exist (it wasn't difficult to > write, even > for me :) ), and a search turns up quite a few posts both on the PHP > doc > comments and elsewhere detailing how to do the same in usersland > PHP. There > are some pretty clear usecases for this, such as doing statistics on a > grouped dataset, as well as grabbing all values from a single-column > SQL > query. It's a very simple, unobtrusive patch. > > 2. One of the PHP implementations "preserved keys." This seems > counterintuitive to me, as there's a high probability of > overwriting, and I > can't think of a usecase where one would want to flatten out an > array, but > keep the keys. Nonetheless, it was implemented by one of the > snippets I saw. > Should the function offer this option with a $preserveKeys = false > optional > param? If so, how should I handle duplicate keys? Failure? Warning? > IMO, > it's not worthwhile, but I wanted to get opinions. > > 3. (this is where the noob comes in) What is the protocol on creating > patches? Should I just run the CVS patch command against a major > revision? I > haven't used that before, but it seems straight forward enough. If > someone > can point me to a writeup of the exact "correct" way to generate a > patch, > I'd appreciate it. 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