Title: PHP Loop only returns last variable
Last modified: January 13, 2019

---

# PHP Loop only returns last variable

 *  [chagene92](https://wordpress.org/support/users/chagene92/)
 * (@chagene92)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/php-loop-only-returns-last-variable/)
 * I have successfully passed my php variables to my javascript code, but it’s only
   grabbing the last variable. I’m assuming it’s my loop that is incorrect but I’ve
   been struggling with this for over a week now so any help would be greatly appreciated.
   What I’m trying to accomplish is that when the name of the custom field is clicked,
   that specific location will drop on the map and replace the old marker with the
   new. I’m still a newbie with all of this.
 * Here is my PHP
 *     ```
       function add_scripts() {
          wp_register_script( 'marker', get_stylesheet_directory_uri() . '/js/marker.js', array('jquery') );
          $posts = get_posts('locations');
          $longs = array();
          $lats = array();
          foreach($posts as $post){
             array_push($lats, rwmb_meta('latitude') );
             array_push($longs, rwmb_meta('longitude') );
          }
          $passedValues = array( 
             'latitude' => $lats, 
             'longitude' => $longs,
          );
          wp_localize_script( 'marker', 'passed_object', $passedValues );
          wp_enqueue_script('marker', get_template_directory_uri() . '/js/marker.js', array('jquery') );
       }
       add_action( 'wp_enqueue_scripts', 'add_scripts' );
       ```
   
 * and here is my Java
 *     ```
       function dropMarker(elem) { //function that will add markers on button click
          var latitude = passed_object.latitude[$( elem ).attr('id')];
          var longitude = passed_object.longitude[$( elem ).attr('id')];
          var marker = new google.maps.Marker({
             position: new google.maps.LatLng(latitude, longitude),
             map: map,
             animation: google.maps.Animation.DROP,
          });
          if (oldMarker != undefined) {
             oldMarker.setMap(null);
          }
          oldMarker = marker;
             map.setCenter(marker.getPosition());
          }
       ```
   
    -  This topic was modified 7 years, 5 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fphp-loop-only-returns-last-variable%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/php-loop-only-returns-last-variable/#post-11084653)
 * > when the name of the custom field is clicked, that specific location will drop
   > on the map
 * I think there’s a disconnect with the name and the value.
    Your PHP is outputting
   an array of arrays with what? numbers? name and number? or does `rwmb_meta` return
   something else? And then your JS is looking up `$( elem ).attr('id')` to use 
   as the subscript. This is what you are saying is the “name of the custom field
   is clicked”, right?
 * You have a loop `foreach($posts as $post){` but you don’t use $posts or $post,
   and they are not global.
 *  Thread Starter [chagene92](https://wordpress.org/support/users/chagene92/)
 * (@chagene92)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/php-loop-only-returns-last-variable/#post-11084666)
 * I used metaboxe.io to create a custom post type and the rwmb_meta is how to get
   the field value of that metabox. They are numbers, but I set the metabox up as
   a text, would that cause an issue?
 * I’m sorry, I guess I should have said when the name of the custom post is clicked,
   the custom field’s value will set the marker’s new coordinates.
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/php-loop-only-returns-last-variable/#post-11084800)
 * The important part is the last line of my answer: you are looping, but not telling
   the other function which one to process.
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/php-loop-only-returns-last-variable/#post-11084818)
 * How does your `rwmb_meta()` function know which post it should use to grab the
   meta data? I suspect you need to pass `$post->ID` to the function, and use that
   ID within the function to obtain the correct meta data.
 *  Thread Starter [chagene92](https://wordpress.org/support/users/chagene92/)
 * (@chagene92)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/php-loop-only-returns-last-variable/#post-11233784)
 * I am kind of starting to understand what you’re saying Joy. So I need to tell
   my javascript function which one to process? I guess I assumed it would just 
   use the active one but that also explains my lack of knowledge for php.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘PHP Loop only returns last variable’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [chagene92](https://wordpress.org/support/users/chagene92/)
 * Last activity: [7 years, 4 months ago](https://wordpress.org/support/topic/php-loop-only-returns-last-variable/#post-11233784)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
