【问题标题】:Traversing through array not working遍历数组不起作用
【发布时间】:2017-06-14 07:46:26
【问题描述】:

json 对象作为变量 $results 中的输出出现

           Array ( [ts] => MongoTimestamp Object ( [sec] => 1497334764 [inc] => 1 ) [t] => MongoInt64 Object ( [value] => 1 ) [h] => MongoInt64 Object ( [value] => -3885623284897060533 ) [v] => 2 [op] => i [ns] => mgl.triggersTbl [o] => Array ( [_id] => MongoId Object ( [$id] => 593f83ec1d7859cc0f000029 ) [Name] => Ahmad [Address] => Delhi ) ) 
           Array ( [ts] => MongoTimestamp Object ( [sec] => 1497349628 [inc] => 1 ) [t] => MongoInt64 Object ( [value] => 8 ) [h] => MongoInt64 Object ( [value] => -3083419979530819825 ) [v] => 2 [op] => i [ns] => mgl.triggersTbl [o] => Array ( [_id] => MongoId Object ( [$id] => 593fbdfc1d78591819000029 ) [Name] => Nehal [Address] => Mumbai ) ) 
           Array ( [ts] => MongoTimestamp Object ( [sec] => 1497428148 [inc] => 1 ) [t] => MongoInt64 Object ( [value] => 9 ) [h] => MongoInt64 Object ( [value] => -660915868390613413 ) [v] => 2 [op] => i [ns] => mgl.triggersTbl [o] => Array ( [_id] => MongoId Object ( [$id] => 5940f0b41d78591414000029 ) [Name] => Seema [Address] => Mumbai ) )

代码是

             $cursor = $c->find(array('ns'=> 'mgl.triggersTbl', 'op' => 'i'));
    // $cursor->timeout(-1);
     $cursor->tailable(true);
     //$cursor->awaitData(true);

     while ($cursor->hasNext()) {
     $cursor->timeout(-1);
     try
     {
         $results =array();
         $results = $cursor->getNext();
         var_dump($results);
     }

现在我正在尝试一一获取这些值,以使其看起来像。

        foreach ($results as $key)
        {   
          foreach ($key as $p => $res)
           {
            echo "Timestamp"." : ". $key["sec"]."<br/>";
            echo "Name"." :".$res["Name"]."<br/>";
            echo "Address"." :".$res["Address"];
          }
        }          

此代码不起作用。它会抛出错误消息。

" 不能使用 MongoTimestamp 类型的对象作为 " echo "Timestamp" 中的数组。" : "。 $key["sec"]."
";"

如果我使用

        foreach ($results as $res)
        {   
            echo "Name"." :".$res["Name"]."<br/>";
            echo "Address"." :".$res["Address"];
        }

仍然抛出错误信息

请帮忙!!!

【问题讨论】:

  • 什么错误信息?
  • 为什么是两个循环?使用foreach ($results as $p =&gt; $res)。在内循环中使用 $key 似乎也很奇怪。
  • 对象数组。您需要使用-&gt; 来访问该属性,而不是索引。
  • 我试过 foreach ($results as $p => $res) { echo "Name"." :".$res["Name"]."
    "; echo "地址"." :".$res["地址"]; }... 它仍然会抛出该错误消息
  • 你能告诉我怎么做吗

标签: php php-5.6 php-5.5


【解决方案1】:

数组中的每个元素都是不同的,因此循环遍历数组中的每个元素是没有意义的。

假设下面是$results,

array(7) { 
    ["ts"]=> object(MongoTimestamp)#6 (2) { 
        ["sec"]=> int(1497334764) 
        ["inc"]=> int(1)
    } 
    ["t"]=> object(MongoInt64)#7 (1) { 
        ["value"]=> string(1) "1"
    } 
    ["h"]=> object(MongoInt64)#8 (1) { 
        ["value"]=> string(20) "-3885623284897060533"
    }
    ["v"]=> int(2) 
    ["op"]=> string(1) "i"
    ["ns"]=> string(15) "mgl.triggersTbl"
    ["o"]=> array(3) {
        ["_id"]=> object(MongoId)#9 (1) {
            ["$id"]=> string(24) "593f83ec1d7859cc0f000029"
        }
        ["Name"]=> string(5) "Ahmad"
        ["Address"]=> string(5) "Delhi"
    }
} 

您可以使用下面的代码从上到下打印所有值。

echo $results["ts"]->sec;
echo $results["ts"]->inc;
echo $results["t"]->value;
echo $results["h"]->value;
echo $results["v"];
echo $results["op"];
echo $results["o"]["_id"]->{'$id'};
echo $results["o"]["Name"];
echo $results["o"]["Address"];

【讨论】:

  • 我希望这是循环,因为 $results 数组中可以有多个相似的 json 对象。
  • @Nida 请告诉我们您是如何准确(什么代码)打印出$results 变量转储的。
  • @Nida 它们完全属于不同的数组吗?请同时发布您用于打印这 3 个数组的代码。
【解决方案2】:

如果您将foreach 用于这种形式的数组,您将遍历值,而不是键。第一个值是您的对象(MongoTimestamp),您可以在第一个echo 中将其作为数组访问。有关其他建议,请参阅 cmets。

更新: 数组有点难以阅读,也许做一个print_r(array_keys($result)) 以确保结构是你所期望的(而不是嵌套更深层次的东西)数组中的数组)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-25
    • 2020-02-12
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多