0

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

3
  • Declare the $lat and $long as array[] You will achieve your goal :) Commented Jun 19, 2015 at 5:30
  • i am not able to pass lat long into $phpArray to create dynamic array Commented Jun 19, 2015 at 5:32
  • What you get if you do print_r($lat) after the while loop ? Commented Jun 19, 2015 at 5:34

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This is really good answer and +1 for warning about mysqli_* depreciation
0
$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:

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.