【问题标题】:How to get a complex value in array?如何在数组中获得一个复杂的值?
【发布时间】:2016-09-21 08:40:23
【问题描述】:

这是我的 JSON 数组

Array
(
    [0] => Array
        (
            [active] => Y
                (
                    [0] => Array
                        (
                            [sub] => 5
                        )
                )
        )
)

我的问题是如何使用foreachsub 中获得价值5

【问题讨论】:

  • 什么是$results?? datatype 在哪里??
  • 预期结果是什么?
  • 为了将来参考,如果您向我们展示的数据与您向我们展示的代码相匹配,将会有所帮助。然后,您不会收到很多问题,询问为什么 2 似乎以任何方式都不匹配。 How to ask a good questionthe perfect question 以及如何创建Minimal, Complete and Verifiable example
  • 你必须更好地解释你想要什么,你的问题是什么以及预期的结果是什么。
  • 当你有可以在浏览器中运行的代码时使用stack sn-ps,如果你有其他代码使用4空格缩进或{}按钮。

标签: javascript php arrays


【解决方案1】:

试试这个:

foreach ($rs_amount as $result) {
    foreach ($result['sub_resource'] as $sub_resource) {
        foreach ($sub_resource as $subs) {
            echo $subs['sub'] . "<br/>";
        }
    }
}

【讨论】:

    【解决方案2】:

    使用这个

    foreach($results['data'] as $result) {
          if(is_array($result)) {
               print_r($result) ;
               echo '<br>';
          } else {
               echo $result['type'] . '<br>';
          }   
    }
    

    【讨论】:

    • 那么做print_r($results['data']); 并完全切断所有foreach 循环会不会更简单
    猜你喜欢
    • 1970-01-01
    • 2020-02-25
    • 2023-03-10
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2020-01-01
    相关资源
    最近更新 更多