Support » Fixing WordPress » Retrieve values from an array

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

    (@keesiemeijer)

    moderator

    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

    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

    Hi

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

    Kevin

    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)

    moderator

    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.