【问题标题】:print_r output as string only returning last itemprint_r 输出为字符串,仅返回最后一项
【发布时间】:2014-11-17 20:46:52
【问题描述】:

我正在使用查询来提取 wordpress 网站所有用户的 geo_latitude 和 geo_longitude。我需要将所有这些作为变量输出,以便将其插入映射插件。

我已经设法将下面的代码放在一起,它与print_r 配合得很好,它在屏幕上打印了 100 多个用户的完整列表。但是,当我尝试将其作为变量输出时,它只返回最终用户的详细信息。

我认为这与foreach 有关,但我不知道如何解决它。

$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);
 }} 

【问题讨论】:

  • 您没有添加位置,只是替换它们。如果要将其分配给不同的变量,则应创建某种数组来存储每个位置。这就是它保留最后一条记录的原因:它是你做的最后一次替换;)

标签: php arrays string wordpress


【解决方案1】:

你必须这样做:

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

如果您只想查看值,每行可以执行以下操作:

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

编辑:我看到添加到变量 $gLocations 也是错误的。这是您的代码的修复:

$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 />";
}

【讨论】:

    【解决方案2】:

    问题是你在循环中初始化一个变量的值,这样如果循环退出,只会存储最后一个值。你应该使用数组

    使用这个

    $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);
     }} 
    

    编辑

    你可以通过两种方法做到这一点

    1. 循环

       foreach($gLocations as $key=>$value){
           echo $value;//'62.7594129,15.425879000000009 |.... so on' will be output
        }// you can also use for loop
      
    2. 普通方法 您可以通过索引访问数组元素

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

    【讨论】:

    • 好的,我看看哪里出错了。现在(请原谅我的新手问题)问题是数组输出为Array ( [0] =&gt; 62.7594129,15.425879000000009 |,我只需要由管道62.7594129,15.425879000000009 | 分隔的纯地理数据。如何省略多余的文字?抱歉,前端开发人员在这里尝试后端的东西
    • 忽略以上。问题解决了,我通过使用implode 去除多余的数组文本得到了正确的输出。最终代码为:$user_lat = get_the_author_meta('geo_latitude', $bloguser-&gt; ID); $user_long = get_the_author_meta('geo_longitude', $bloguser-&gt;ID ); array_push($gLocations, $user_lat .',' .$user_long. ' | '); } $user_out=implode("",$gLocations); }}
    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 2017-01-26
    • 2021-08-14
    • 1970-01-01
    • 2022-07-06
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多