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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php