Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Boston
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Sending JSON array data correctly for lat/long

    Hello, I'm sending arrays of data that eventually make it my JS file. Everything works fine when using my new object there from my php.

    PHP Code:
    foreach ($q->posts as $post) {
        
    $array[$i] = array();
        
    $array[$i]['post_coords'] = get_post_meta($post->ID'loc_coords'true);
        
    $i++;
    }
    echo 
    json_encode($array); 
    But since lat/long comes in one string of text I can't seem to grab it in my JS like:

    PHP Code:
    object.coords[0
    object.coords[1
    I just get the first and second number from something like this: 41.99990, -71.39923

    Should I be sending it this way or should I be doing something I'm not aware of in JS?

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    74
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Could you explode it by the comma and return that array?

    At the moment it seems you are trying to access a string like an array so it is only returning the first and second character of the string.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You should explode it like davidjones1990 suggests. Something like:

    PHP Code:
    foreach ($q->posts as $post)
    {
        
    $array[$i] = array();
        list( 
    $lat$lon ) = explode','get_post_meta$post->ID'loc_coords'true ) );
        
    $array[$i]['post_coords']['lat'] = $lat;
        
    $array[$i]['post_coords']['lon'] = $lon;
        
    $i++;

    This will return one key for each coordinate inside of your object in JSON & JS.

  • #4
    New Coder
    Join Date
    Jul 2012
    Location
    Boston
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Explode()

    Thanks guys. Had never used explode() before.

    PHP Code:
    $split get_post_meta($post->ID'loc_coords'true); 
            
    $coords explode(","$split);
            
    $array[$i]['lat'] = $coords[0];
            
    $array[$i]['lon'] = $coords[1]; 

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    Quote Originally Posted by Webenvelopment View Post
    Thanks guys. Had never used explode() before.

    PHP Code:
    $split get_post_meta($post->ID'loc_coords'true); 
            
    $coords explode(","$split);
            
    $array[$i]['lat'] = $coords[0];
            
    $array[$i]['lon'] = $coords[1]; 
    If you build an array with javasacript, and set that array as the value of an input element, and submit it, it will show up on the server side, in php at least, as a comma separated list, and can be exploded like that. If you have multiple arrays to upload, just separate them by something else, like a | and explode them to get the sub lists out.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •