ankita simlai
ankita simlai

Reputation: 37

create dynamic array within a array php

I have an sql query that returns the altitude of a place. I have exploded the result to separate longitude and latitude. The result is stored in array.

Now I want to create an array which should contain all the arrays returned by the explode function.

$sql_altitude = mysql_query("SELECT altitude FROM `navigatio_info`
                                             WHERE bus_id='$bus_id'
                                             AND driver_id ='$driver_id' 
                                             ORDER BY stop_no ASC 
                                             LIMIT 0 , 30");
while ($row = mysql_fetch_assoc($sql_altitude)) 
 {
   //echo $row['altitude'];
   //$altitude=array();
   $altitude=(explode("-",$row['altitude']));
   print_r($altitude);
   //$lat=array();
   $lat=$altitude[0];
   //print_r($lat);
   echo '<br/>';
   //$long=array();
   $long=$altitude[1];
   //print_r($long);
   //echo '<br/>';
}

below is a static array defined:

 <?php 
   $phpArray = array(array('Vadodara',22.3000,73.2000,5),
                     array('Valsad',20.6300,72.9300,2),
                     array('Thane',19.1724,72.9570,1));
 )?>

I want $phpArray to have dynamic values generated from the query above

Upvotes: 0

Views: 153

Answers (3)

Samit Khulve
Samit Khulve

Reputation: 164

as i understand your requirement use the following code.

$phpArray = array();
while ($row = mysql_fetch_assoc($sql_altitude)) {
       $altitude=(explode("-",$row['altitude']));
       $phpArray[]= $altitude;
}

print"<pre>";
print_r($phpArray);
print"</pre>";

above code will generate following array.

Array
(
[0] => Array
    (
        [0] => Vadodara
        [1] => 22.3000
        [2] => 73.2000
        [3] => 5
    )

[1] => Array
    (
        [0] => Valsad
        [1] => 20.6300
        [2] => 72.9300
        [3] => 2
    )

[2] => Array
    (
        [0] => Thane
        [1] => 19.1724
        [2] => 72.9570
        [3] => 1
    )

)

Hope this helps.

Upvotes: 0

Akshay Hegde
Akshay Hegde

Reputation: 16997

Please use mysqli_* functions since mysql_* functions are old now.

$phpArray = array();
while ($row = mysql_fetch_assoc($sql_altitude)) 
{
   /* 
       I assume $row['altitude'] contains something like below string
       $row['altitude'] = "place-latitude-longitute-altitude"
   */
     $phpArray[] = explode("-",$row['altitude']);
}

print_r($phpArray);

Upvotes: 1

Ramki
Ramki

Reputation: 519

$sql_altitude = mysql_query("SELECT altitude FROM `navigatio_info` WHERE bus_id='$bus_id'AND driver_id ='$driver_id' ORDER BY stop_no ASC LIMIT 0 , 30");
$phparray = array();
while ($row = mysql_fetch_assoc($sql_altitude)) 
{
$altitude=(explode("-",$row['altitude']));
$lat=$altitude[0];
$long=$altitude[1];
$phparray = array($lat,$lat);
}
echo "<pre>";
print_r($phparray)
echo "<pre>";

Please check above code:

Upvotes: 0

Related Questions