Creating multi-dimensional array from MySql Result set

Hi.

I need to create the following array like the one below, my data is coming from a mysql query and is therefore ‘flat’.


Array
(
    [0] => Array
        (
            [0] => padova
            [1] => padova
            [2] => Array
                (
                    [baloon] =>
Latitude: %LAT%
Longitude: %LON%

                    [zindex] => 3
                    [title] => just the title
                )

        )

    [1] => Array
        (
            [0] => verona
            [1] => verona
            [2] => Array
                (
                    [baloon] =>
Latitude: %LAT%
Longitude: %LON%

                    [zindex] => 3
                    [title] => just the title
                    [onclick] => function(lat, lon){
                    document.getElementById("debug").innerHTML("Lat: "+lat+"\
Lon: "+lon);
                }
                )

        )

    [2] => Array
        (
            [0] => cesena
            [1] => cesena
            [2] => Array
                (
                    [openbaloon] =>
Cheers from Cesena !!! :D

                    [zindex] => 3
                    [title] => just the title
                    [icon] => http://www.freakstyle.it/classes/gmapsv3/samples/img/beachflag.png
                    [icon_size] => 20,32
                    [shadow] => http://www.freakstyle.it/classes/gmapsv3/samples/img/beachflag_shadow.png
                    [shadow_size] => 37,32
                )

        )

)

I am completly stumped how to loop through my mysql result set and generate the arrays… can anyone help.

Thanks
Chris

What does the result set look like? And why are the arrays inside the main array all different?

Hi.

The array is for placing markers on google maps and the bottom array is different because it has different options enabled, its not really relevant in my case as I am after just the basic array building to place markers on the map.

The data set is as per the image attached…

Ok, and what would the relation be between your table column names and the array elements in the example you posted?

You can create a 2D array from a result set with something like this:


[COLOR=#000000][COLOR=#0000BB]$my2Darray [/COLOR][COLOR=#007700]= array();

while([/COLOR][COLOR=#0000BB]$row [/COLOR][COLOR=#007700]=  [/COLOR][COLOR=#0000BB]mysql_fetch_assoc[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$result[/COLOR][COLOR=#007700])) { 
    [/COLOR][/COLOR][COLOR=#000000][COLOR=#0000BB]$my2Darray[/COLOR][/COLOR][COLOR=#000000][COLOR=#007700][] = [/COLOR][COLOR=#0000BB]$row[/COLOR][COLOR=#007700]; 
}[/COLOR][/COLOR]

As you’re looping through the result set, each row of the result set is added as a new row (array) in $my2Darray.

Hi.

At this point in time, Im not bothered what content goes into the arrays, I am more intrested in the actual creating of the array… That said the basics of what I am after is


$testArray = array(array(
            $sqlData['countryname'],
            array('lat'=>$sqlData['lat'], 'lon'=>$sqlData['longitude'])
            array(
                'baloon'=>'<div>Url:'.$sqlData['trackedUrl'].'<br /> Country Code:'.$sqlData['countrycode'].'</div>',
                'zindex'=>3,
                'title'=>$sqlData['countryName'],
            )
        )
    );

The above is just the one itteration and I would need it to loop for as many rows as needed.

Thanks
Chris

Adapt webdev1958’s example:


$testArray = array();

while($sqlData =  mysql_fetch_assoc($result)) { 
    $testArray[] = 
        array(
            $sqlData['countryname'],
            array('lat'=>$sqlData['lat'], 'lon'=>$sqlData['longitude']),
            array(
                'baloon'=>'<div>Url:'.$sqlData['trackedUrl'].'<br /> Country Code:'.$sqlData['countrycode'].'</div>',
                'zindex'=>3,
                'title'=>$sqlData['countryName']
            )
        );
}