• Resolved kevin heath

    (@ypraise)


    Hi,

    I’ve spent 2 days going crazy trying to retrieve a couple of vlaues from an array so I can display a google map.

    I may end up re-wrting the way that users submit the form but if someone can offer a bit of advice first then it will be ideal.

    The co-ordinates are sotred in an array called markers I know that the co-ordinates are being stored because when I run the following code;

    $custom_fields = get_post_custom_values('twitcher');
      $my_custom_field = $custom_fields;
      foreach ( $my_custom_field as $key => $value )
        echo "Retrieve co-ordinates - " . $key . " => " . $value . "<br />";

    I get all the correct values printed out as such:

    Retrieve co-ordinates – 0 => a:9:{s:5:”title”;s:2:”zx”;s:4:”date”;s:10:”27/08/2012″;s:4:”body”;s:3:”xxz”;s:7:”markers”;a:1:{i:0;a:2:{i:0;s:7:”51.6317″;i:1;s:7:”-3.0378″;}}s:4:”zoom”;s:1:”7″;s:10:”image_file”;s:19:”twitcher_image_file”;s:8:”category”;s:3:”268″;s:4:”name

    What I need to do is pull out those marker co-ordinates so I can send them to the google api.

    anyone help?

    thanks
    Kevin

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You are trying to get the “markers” value from a serialized array (which is a string).

    try it with this [untested]:

    foreach ( $my_custom_field as $key => $value ) {
      // WordPress function to unserialize (make it an array if it's a serialized array)
      $serialized = maybe_unserialize( $value );
      if(is_array($serialized) && isset($serialized['markers'])) {
    
          // do stuff for your markers array
          echo '<pre>';
          print_r($markers);
          echo '</pre>';
    
      }
    }

    http://codex.wordpress.org/Function_Reference/maybe_unserialize

    Thread Starter kevin heath

    (@ypraise)

    Hi keesiemeijer
    thanks for the suggestion. It looks like another new wordpress function to learn about 😉

    It’s partially worked and did solve on of the problems I want to solve.

    The problem is the the markers in an array within an array. Though with a bit of changes to your code I’ve been able to pull in the zoom level from the serialized data and pass it to the google api.

    The code I used was

    $custom_fields = get_post_custom_values('twitcher');
      $my_custom_field = $custom_fields;
      foreach ( $my_custom_field as $key =>$value){
        $serialized = maybe_unserialize( $value );
    if(is_array($serialized) && isset($serialized['zoom'])) {
    
        echo "Retrieve co-ordinates - " . $serialized['zoom'] . "<br />";
    }
    }

    I was able to pass the zoom to the google maps api with:

    var myOptions = {
                    zoom: <?php echo isset($serialized['zoom']) ? $serialized['zoom'] : 6 ?>,
    }

    I’m not quite sure how to pull out a serialized array within a serialized array.

    thanks
    Kevin

    Thread Starter kevin heath

    (@ypraise)

    Hi

    I should just say that if I put in markers instead of zoom then I only get back the word ARRAY

    Kevin

    Thread Starter kevin heath

    (@ypraise)

    Hi

    Thanks I’ve got it sorted on how to retrieve the values. Thanks keesiemeijer

    $custom_fields = get_post_custom_values('twitcher');
      $my_custom_field = $custom_fields;
      foreach ( $my_custom_field as $key =>$value){
        $serialized = maybe_unserialize( $value );
    if(is_array($serialized) && isset($serialized['zoom'])) {
    
        echo "Retrieve zoom - " . $serialized['zoom'] . "<br />";
    }
    }
    
     foreach ( $my_custom_field as $key =>$value){
        $serialized = maybe_unserialize( $value );
    if(is_array($serialized) && isset($serialized['markers'])) {
    
     $markers = $serialized['markers'];
    foreach($markers as $marker_latlng){
    
                            $lat = $marker_latlng[0];
                            $lng = $marker_latlng[1];
                        }
    
        echo "Retrieve co-ordinates - " . $marker_latlng[0] . "<br />";
    	  echo "Retrieve co-ordinates - " . $marker_latlng[1] . "<br />";
    }
    }
    Moderator keesiemeijer

    (@keesiemeijer)

    Well done! I’m glad you found a solution 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Retrieve values from an array’ is closed to new replies.