Re: Fast code to partially flatten an array based on several keys?

From: Date: Fri, 28 Jun 2002 04:03:25 +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-104258@lists.php.net to get a copy of this message
Hi I would do it this way (untested) while ($row = mysql_fetch_assoc($result)) {
        $vehicles[$row['dealer_id][$row['vehicle_id]['views'][] = $row['page_views'];
} should end up something like this ( [645] => Array
        (
        [35073] => Array
                ([
                [views]=> Array
                        (
                        [1] => 10
                        [0] => 6
                )
        )
) Tom At 01:09 PM 28/06/2002 +1200, Morgan Grubb wrote:
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


« previous php.general (#104258) next »