【问题标题】:Get values from keys from a decode Json array?从解码 Json 数组中的键获取值?
【发布时间】:2020-01-25 12:13:43
【问题描述】:
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [artRef] => TEX00006
                    [artDesc] => ESPONJA MICROFONE FACE CORTADA P. SCALA RIDER/SRC
                    [qttStock] => 82
                    [dateStock] => 2020-01-25T11:59:26.2337248+00:00
                )

            [1] => Array
                (
                    [artRef] => TEX00006
                    [artDesc] => ESPONJA MICROFONE FACE CORTADA P. SCALA RIDER/SRC
                    [qttStock] => 1
                    [dateStock] => 2020-03-31T00:00:00Z
                )

        )
)

这是我的解码 json 数组,现在我想在循环或 foreach 中从 qttStock 和 dateStock 获取值?我只能获取第一个数组 [0] 如果数组有多个 [qttStock] 等,我如何获取所有值?

【问题讨论】:

  • foreach ($array[0] as $innerArray)
  • 数组嵌套2层深有原因吗?
  • 使用嵌套循环处理数组的两个级别。

标签: php arrays json decode


【解决方案1】:

使用嵌套循环。

foreach ($array as $subarray) {
    foreach ($subarray as $element) {
        echo "{$element['qttStock']} on {$element['dateStock']}<br>";
    }
}

【讨论】:

    【解决方案2】:

    感谢您的提问! 请尝试:

    for($x=0;$x<count($array);$x++){
        echo $array[0][$x]['qttStock'];
        echo $array[0][$x]['dateStock'];
    }
    

    【讨论】:

      【解决方案3】:

      嗨@Barmar,但是当数组只有一个级别时,我会收到很多警告

      Array
      (
          [0] => Array
              (
                  [artRef] => 4990003325
                  [artDesc] => VISEIRA SCHUBERTH C3/S2 PREP. P/ PINLOCK ESPELHADA (IRIDIUM)
                  [qttStock] => 2
                  [dateStock] => 2020-01-25T13:23:45.3420766+00:00
              )
      
      )
      

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移“qttStock”

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移量“dateStock” 4对4

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移“qttStock”

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移量“dateStock” V上V

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移“qttStock”

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移量“dateStock” 2对2

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移“qttStock”

      警告:第 156 行 /home3/mvital/public_html/app/siemet/wp-content/plugins/woo-show-stock-from-xml/inc/functions-api.php 中的非法字符串偏移量“dateStock” 2对2

      【讨论】:

        猜你喜欢
        • 2019-03-22
        • 2018-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-15
        • 2021-12-04
        • 1970-01-01
        • 2022-07-07
        相关资源
        最近更新 更多