【问题标题】:foreach loop nested array json phpforeach循环嵌套数组json php
【发布时间】:2018-10-23 13:29:12
【问题描述】:

如何循环遍历数组以获取“converted_amount”值?

stdClass Object
(
[rows] => Array
    (
        [0] => stdClass Object
            (
                [components] => Array
                    (
                        [0] => stdClass Object
                           (
                                [amount] => 5033298.132349431
                                [count] => 1337
                                [rate] => 3.1398800
                                [converted_amount] => 1603021.9952863243
                            )

                        [1] => stdClass Object
                            (
                                [amount] => 458673.0026585825
                                [count] => 325
                                [rate] => 0.45260800
                                [converted_amount] => 1013400.4157520011
                            )

我尝试过这样的 foreach,但它不起作用。我认为应该在组件和converted_amount 之间有一些东西——也许是另一个foreach?我不知道。

foreach ($getexvolume as $vol) {
echo $vol['rows'][0]['components']['converted_amount'];}

【问题讨论】:

  • 不知道是否有帮助,但据我所知,您的回声应该是 $vol->rows[0]->components[0]->converted_amount

标签: php arrays json foreach nested


【解决方案1】:

你有一个对象而不是数组。您必须将数据作为对象来处理...

foreach ($getexvolume->rows as $row) {
    foreach ($row->components as $component) {
       echo $component->converted_amount;
    }
}

【讨论】:

  • 谢谢 - 这看起来不错,但由于某种原因它没有回应任何东西 - 它是空白的
  • @Vikki,请附上 $getexvolume 变量的 var_dump
  • object(stdClass)#1137 (3) { ["result"]=> string(7) "success" ["count"]=> int(1) ["rows"]=> array(1) { [0]=> object(stdClass)#1136 (6) { ["components"]=> array(33) { [0]=> object(stdClass)#1135 (6) { ["base "]=> object(stdClass)#1134 (2) { ["currency"]=> string(3) "CNY" ["issuer"]=> string(34) "rPT74sUcTBTQhkHVD54WGncoqXEAMYbmH7" } ["counter"]=> object(stdClass)#1133 (1) { ["currency"]=> string(3) "XRP" } ["amount"]=> string(17) "5033298.132349431" ["count"]=> int(1337) ["rate"]=> string(9) "3.1398800" ["converted_amount"]=> string(18) "1603021.9952863243" }
【解决方案2】:
   echo $vol->rows[0]->components[0]->converted_amount;

您正在混合数组和对象。您的输出是一个对象,因此您必须像访问它一样访问它,否则如果您想将其视为数组,则必须将其转换为数组。至于现在你可以使用上面的代码。

我认为适合您问题的更好解决方案是您循环嵌套数组,例如:

foreach($vol->rows[0]->components as $data){
echo $data->converted_amount;
}

【讨论】:

    【解决方案3】:

    试试这个:

    foreach ($getexvolume->rows[0]->components as $vol) {
           echo $vol->converted_amount;
    }
    

    【讨论】:

      【解决方案4】:

      您拥有的对象是数组和对象的混合体。 数组可以寻址为$array['value'],但对象必须寻址为$object->value

      echo $vol->rows[0]->components[0]->converted_amount;

      但是,由于您有多个组件,因此您将需要一个嵌套循环:

      foreach ($getexvolume as $vol)
      {
        foreach($vol->rows as $row)
        {
          foreach($row->component as $component)
          {
            echo $component->converted_amount;
          }
        }
      }
      

      (伪代码 - 未测试)。

      理想情况下,首先将变量标准化为多维数组或嵌套对象,这样您就不必担心语法问题。

      【讨论】:

        猜你喜欢
        • 2016-07-25
        • 2015-07-17
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        • 2015-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多