1

I am trying to build an array from entries in a MySQL database. I have connected to the database just fine, and I have a foreach loop which pulls the entries from the database based on the quantity of items like so:

$totalMarkers = count($results);

foreach($results as $result){
$gpsLats[] = $result->gpslat;
$gpsLongs[] = $result->gpslong;
}

I then need to take these entries and run them through a while loop to attempt to build my array:

    $it = 0;

while ($it < $totalMarkers) {
    $incr = $it++;
    $myLatitudes = $gpsLats[$incr];
$myLongitudes = $gpsLongs[$incr];

$items = array($myLatitudes,$myLongitudes);

  print_r($items);

}

The problem is that the output looks something like this:

Array ( [0] => 54.8607 [1] => -32.4135 ) 
Array ( [0] => 39.8460 [1] => -87.4166 )
Array ( [0] => 78.8403 [1] => -95.4156 ) 

Really what I need is for all the entries to be contained in one array statement. I have a good feeling that I am overcomplicating this, but I need a nudge in the right direction. Thanks for your time in looking into this.

2
  • 1- $items[] = array($myLatitudes,$myLongitudes); 2- move print_r($items); to outside the while loop Commented Apr 14, 2014 at 19:10
  • Would it not be easier to build the $items array in your first foreach loop instead of running through everything twice? Commented Apr 14, 2014 at 19:18

1 Answer 1

2

You forgot the array 'append' operation:

$items[] = array($myLatitudes,$myLongitudes);
      ^^--- missing

Without the [], you're simply creating a two-item array and overwriting it on every loop iteration.

Sign up to request clarification or add additional context in comments.

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.