Re: Fast code to partially flatten an array based on several keys?
| From: | Morgan Grubb | Date: | Fri, 28 Jun 2002 01:51:19 +0000 |
| Subject: | Re: Fast code to partially flatten an array based on several keys? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-104252@lists.php.net to get a copy of this message | ||
Martin,
That is a little scary. But you've fallen into the same trap that I wound up
in in that it's too slow on big queries. =/
Your second idea was something I experimented with earlier, but due to the
nature of some of the other data I'm trying to work with didn't really work
out too well.
Although I have, just now, thought of a way where I could do that second
suggestion, and then manually go through the created array and reindex it in
one pass, which would hopefully give me something I can use.
Now this is an interesting train of thought, and is liable to drive me
clinically insane.
I'll let you know how I get on. =)
Cheers,
Morgan Grubb.
"Martin Towell" <martin.towell@world.net> wrote in message
news:6416776FCC55D511BC4E0090274EFEF508A550@EXCHANGE...
> This is what I came up with before I saw this email, and "eek", we think
the
> same (sorta)
>
> This is going to be a bit iffy, but here goes (BTW: this hasn't been
tested,
> so expect heaps of errors :) )
>
> $i = 0;
> while ($row = mysql_fetch_assoc($result)) {
> for ($j = 0, $found = false; $j < $i && !$found; $j++) {
> if ($vehicles[$j]['dealer_id'] == $row['dealer_id'] &&
> $vehicles[$j]['vehicle_id'] == $row['vehicle_id']) {
> $found = true;
> $idx = $j
> }
> }
> if (!$found) {
> $idx = $i;
> $i++;
> }
> $vehicles[$idx]['dealer_id'] = $row['dealer_id'];
> $vehicles[$idx]['vehicle_id'] = $row['vehicle_id'];
> $vehicles[$idx][$row['company_id']] = $row['page_views'];
> }
>
> The other thing I just thought of is: can $row['dealer_id'] and
> $row['vehicle_id'] be indexes to the array
>
$vehicles[$row['dealer_id']][$row['vehicle_id']][$row['company_id']]
> = $row['page_views'];
>
> so you'd get something like (manually created result...):
> (
> [645] => Array
> (
> [35073] => Array
> (
> [1] => 10
> [0] => 6
> )
> )
> )
>
> -----Original Message-----
> From: Morgan Grubb [mailto:morgan@autobuy.co.nz]
> Sent: Friday, June 28, 2002 11:35 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Fast code to partially flatten an array based on
> several keys?
>
>
> Martin,
>
> Here's an example of what I implemented (simplified down again).
>
> As you can see, it's hideously simple and not optimized in the least.
>
> It works well on small datasets, but you can see by looking at it that
large
> datasets get exponentially slower.
>
> while ($row = mysql_fetch_assoc($result)) {
> $found = false;
> $count = count($vehicles);
> for ($j = 0; $j < $count; $j++) {
> if ( $vehicles[$j]['dealer_id'] == $row['dealer_id']
> &&
> $vehicles[$j]['vehicle_id'] == $row['vehicle_id'] ) {
> $vehicles[$j][$row['company_id']] = $row['page_views'];
> $found = true;
> }
> }
> if (!$found) {
> $vehicles[$i]['dealer_id'] = $row['dealer_id'];
> $vehicles[$i]['vehicle_id'] = $row['vehicle_id'];
> $vehicles[$i][$row['company_id']] = $row['page_views'];
> }
>
> $i++;
> }
>
>
>
> Morgan Grubb.
>
>
> "Martin Towell" <martin.towell@world.net> wrote in message
> news:6416776FCC55D511BC4E0090274EFEF508A54E@EXCHANGE...
> > this sort of merging would be easier to do when you create the array
> > (unless you need the other format for something else?)
> >
> > -----Original Message-----
> > From: Morgan Grubb [mailto:morgan@autobuy.co.nz]
> > Sent: Friday, June 28, 2002 11:09 AM
> > To: php-general@lists.php.net
> > Subject: [PHP] Fast code to partially flatten an array based on several
> > keys?
> >
> >
> > I have a large dataset which can contain many different fields, and I'm
> > trying to flatten them in such a way that I can display it happily in a
> > table.
> >
> > To simplify: I'm populating the array such (many many more fields, but
> these
> > will do):
> >
> > $i = 0;
> > while ($row = mysql_fetch_assoc($result)) {
> > $vehicles[$i]['dealer_id'] = $row['dealer_id'];
> > $vehicles[$i]['vehicle_id'] = $row['vehicle_id'];
> > $vehicles[$i][$row['company_id']] = $row['page_views'];
> > $i++;
> > }
> >
> > Now, if that vehicle_id and dealer_id combination exist, instead of
adding
> > it as another record completely,
> > just add
> >
> > $vehicles[$existing_id][$row['company_id']] = $row['page_views'];
> >
> > The end result of this would be (if given two 'company_id' records for
the
> > given dealer):
> >
> > (
> > [0] => Array
> > (
> > [dealer_id] => 645
> > [vehicle_id] => 35073
> > [1] => 10
> > )
> >
> > [1] => Array
> > (
> > [dealer_id] => 645
> > [vehicle_id] => 35073
> > [0] => 6
> > )
> > )
> >
> > Turns into:
> >
> > (
> > [0] => Array
> > (
> > [dealer_id] => 645
> > [vehicle_id] => 35073
> > [1] => 10
> > [0] => 6
> > )
> > )
> >
> > Can anyone think of a fast way to do this either during creation of the
> > array or after the array has been created? (A sort of
fast_flatten($array)
> > function) ?
> >
> > If anyone can think of something, I'd be really appreciative.
> >
> >
> >
> > Cheers,
> > Morgan Grubb.
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php