Paul Aston
Paul Aston

Reputation: 135

print_r output as string only returning last item

I'm using a query to extract the geo_latitude and geo_longitude for all the users of a wordpress site. I need to output all of this as a variable so I can insert it into a mapping plugin.

I've managed to put together the code below, and it works well enough with print_r, and it prints on screen the full list of 100+ users. However when I try to output it as a variable, it only returns the details of the final user.

I think it's something to do with foreach, but I can't see how to fix it.

$blogusers = get_users_of_blog();
if ($blogusers) {
$excluded_users = array(1, 10, 11);
foreach ($blogusers as $bloguser) {
if (!in_array($bloguser->user_id, $excluded_users))
{
$user = get_userdata($bloguser->user_id, $excluded_users);

 $user_lat = get_the_author_meta('geo_latitude', $bloguser-> ID);
 $user_long = get_the_author_meta('geo_longitude', $bloguser->ID );

 $gLocations = ($user_lat .',' .$user_long. ' | ' );
 }

$var_users = print_r($gLocations, true);
print_r($gLocations);
 }} 

Upvotes: 2

Views: 763

Answers (2)

StaticVariable
StaticVariable

Reputation: 5283

The problem is that you are initializing a value to a variable in loop so that if loop exits only last value will be stored.You should use array

use this

$blogusers = get_users_of_blog();
$gLocations =array();
if ($blogusers) {
$excluded_users = array(1, 10, 11);
foreach ($blogusers as $bloguser) {
if (!in_array($bloguser->user_id, $excluded_users))
{

$user = get_userdata($bloguser->user_id, $excluded_users);

 $user_lat = get_the_author_meta('geo_latitude', $bloguser-> ID);
 $user_long = get_the_author_meta('geo_longitude', $bloguser->ID );
 array_push($gLocations, $user_lat .',' .$user_long. ' | ');
 }

$var_users = print_r($gLocations, true);
print_r($gLocations);
 }} 

EDIT

You can do this by two method

  1. Loop

     foreach($gLocations as $key=>$value){
         echo $value;//'62.7594129,15.425879000000009 |.... so on' will be output
      }// you can also use for loop
    
  2. Normal method you can just access the array element by index

         echo $gLocations[0]; // '62.7594129,15.425879000000009 |' will be output but only one value will be shown
    

Upvotes: 3

Jeroen de Jong
Jeroen de Jong

Reputation: 337

You have to do something like this:

foreach($gLocations AS $key=>$value) {
  echo $key . " -> " . $value;
}

If you only want to see the values, one line each you can do the following:

foreach($gLocations AS $value) {
  echo $value . "<br />";
}

EDIT: I see that the adding to the variable $gLocations is wrong too. Here's a fix for your code:

$blogusers = get_users_of_blog();
if ($blogusers) {
$gLocations = array();
$excluded_users = array(1, 10, 11);
foreach ($blogusers as $bloguser) {
if (!in_array($bloguser->user_id, $excluded_users))
{
$user = get_userdata($bloguser->user_id, $excluded_users);

 $user_lat = get_the_author_meta('geo_latitude', $bloguser-> ID);
 $user_long = get_the_author_meta('geo_longitude', $bloguser->ID );

 $gLocations[] = ($user_lat .',' .$user_long. ' | ' );
 }
 }

$var_users = print_r($gLocations, true);
print_r($gLocations);
 } 
 foreach($gLocations AS $value) {
  echo $value . "<br />";
}

Upvotes: -1

Related Questions