Jump to content

Recommended Posts

Please help, I am trying to get data out of my database as an array but it doesn't seem to be working?!

 

I just get: ArrayArrayArrayArrayArray

 

here is my code

<?php
    $mysqli = mysqli_connect("localhost", "root", "", "plods");
    $sql_locations = "SELECT * FROM southdowns";
    $rs_locations = mysqli_query($mysqli, $sql_locations);
    foreach( $rs_locations as $rs_location ) {
      $markers[] = array(
          "{$rs_location['address1']}, {$rs_location['address2']}",
          $rs_location['Longitude'],
          $rs_location['Latitude']
      );
      echo $markers;
    }
    ?>

Edited by jarv
Link to comment
https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/
Share on other sites

You can't "echo" an array (it will just output the word Array as you have seen. You either need to iterate through the array elements and echo the values or you can use print_r() to output the whole array.

 

Also, do the string concatenation in the query instead of the php logic.

 

Try this

<?php
    $mysqli = mysqli_connect("localhost", "root", "", "plods");
    $sql_locations = "SELECT CONCAT(address1, ', ', address2) as address, Longitude, Latitude FROM southdowns";
    $rs_locations = mysqli_query($mysqli, $sql_locations);
 
    while( $rs_location = $rs_locations->fetch_assoc() ) {
        $markers[] = $rs_location;
    }
 
    echo "<pre>".print_r($markers, 1)."</pre>";
?>
Edited by Psycho

it needs to be out put like this: 

 

array(
'Palace of Westminster, London',
51.499633,
-0.124755
),
array(
'Westminster Abbey, London',
51.4992453,
-0.1272561
)

 

but I am getting: 

Array ( [0] => Array ( [address] => Quintessence Fragrances Ltd, Unit 2A, Hawthorne Industrial Estate [Longitude] => 0.058305 [Latitude] => 50.798006 ) [1] => Array ( [address] => Nyetimber, Broughton House, 6-8 Sackville Street [Longitude] => -0.137792 [Latitude] => 51.509285 )
Edited by jarv

 

no no, if you look at the working example on that link

 

Um, no. You are incorrect. The "working example" on that page is PHP code that defines an hard-coded array for testing purposes instead of getting the values from a DB. The author even includes the DB logic commented out showing that is how it would really be done. Plus, the way an array is defined looks different than when you output an array using print_r(). The print_r() function is really more of a debugging tool to output an array showing the structure and the values. Expecting that to look exactly like the definition of the array is foolish. The only difference between the hard-coded array in that example and the array built from the code I provided is that the array in that example code has numerically based indexes (i.e. 0, 1, 2) rather than named indexes as will be present from a DB query.

 

I don't know if the values of the indexes will matter or not. But, if they do need to be numerical indexes, then convert the array to remove the named indexes and replace with numerical ones. I would show you how to do that, but apparently you know what you are doing, and don't need any assistance from us.

 

EDIT: And just to be 100% clear. The "JSON Array" code that you are pointing to is not JSON. It is simply the definition of a PHP array. It would still need to be converted to JSON (if that's really what you need).

Edited by Psycho

ok, I don't get it, sorry. I am just trying to follow the example using the php array they commented out
 
here is where I currently am:
 
 

$mysqli = mysqli_connect("localhost", "root", "", "plods");
    $sql_locations = "SELECT CONCAT(address1, ', ', address2) as address, Longitude, Latitude FROM southdowns";
    $rs_locations = mysqli_query($mysqli, $sql_locations);
 
    while( $rs_location = $rs_locations->fetch_assoc() ) {
      foreach( $rs_locations->fetch_assoc() as $rs_location => $value){
        $markers[] = $value;
      }
    }
    //while( $rs_location = $rs_locations->fetch_assoc() ) {
    //    $markers[] = $rs_location;
    //}
 
    echo "<pre>".print_r($markers)."</pre>";

and it comes out like this:
 

Array ( [0] => Nyetimber, Broughton House, 6-8 Sackville Street [1] => -0.137792 [2] => 51.509285 [3] => Baker Tilly, The Portland Building, 25 High Street.....
Edited by jarv

You didn't say if key names were required or not, so you want either

    while( $rs_location = $rs_locations->fetch_assoc() ) {
        $markers[] = $rs_location;
    }

or


    while( $rs_location = $rs_locations->fetch_row() ) {
        $markers[] = $rs_location;
    }

followed by

$json = json_encode($markers);
  • Like 1

Unfortunately for you, not all appear to be reversed. I have spotted a dozen in the above data that seem to be the correct way round (although in the North Atlantic).

 

For example, the first three that are visible in the above image.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.