【问题标题】:Outputting multidimensional json array to table php [multiple values]将多维json数组输出到表php [多个值]
【发布时间】:2015-01-05 14:20:09
【问题描述】:

在过去的 9 天里,我一直在做这件事,我这辈子都想不通。

首先通过 cURL 获取 JSON 数组

$result = curl_exec($h);
curl_close($h);
var_dump(json_decode($result, true));

的输出如下:
array(1) {
["SE"]=>
array(4) {
    ["errors"]=>
    array(0) {
    }
    ["billwith"]=>
    string(10) "removedInteger"
    ["bill_detail"]=>
    array(1) {
        ["bill_item"]=>
        array(48) {
          [0]=>
          array(7) {
            ["type1identifier_id"]=>
            string(5) "removed coded string "
            ["prod"]=>
            string(15) "removed string"
            ["charge"]=>
            string(4) "1.36"
            ["misc_date"]=>
            string(6) "063014"
            ["misc_date2"]=>
            string(6) "000000"
            ["notes"]=>
            string(0) ""
            ["color"]=>
            string(4) "hide"
          }
          [1]=>
          array(7) {
          ["type1identifier_id""]=>
          string(5) "CP024"
          ["prod"]=>
          string(15) "removed string "
          ["charge"]=>
          string(3) ".00"
          ["misc_date"]=>
          string(6) "063014"
          ["misc_date2"]=>
          string(6) "000000"
          ["notes"]=>
          string(0) ""
          ["color"]=>
          string(4) "hide"

我正在尝试将此信息写入表格以使其清晰易读,例如“删除的字符串”是我希望在标签中写入/回显的值之一,但我似乎无法取消引用任何在这个烂摊子里。

非常感谢任何帮助,谢谢!

最后一次更新,比较时每次都返回false,不太清楚为什么,我需要先声明我的值吗?这是我正在使用的代码:

if ($bill_item['charge'] == "attention")
{
    echo '<tr bgcolor="red">';
    echo '<td>';
    echo 'charge amount DOES equal attention';
    echo '</td>';
}
  else
  {
    echo '<tr bgcolor="white">';
    echo '<td>';
    echo 'charge amount does NOT equal attention';
    echo '</td>';
  }

【问题讨论】:

  • $array["SE"]["bill_detail"]["bill_item"][0] .. [1] 等不起作用?

标签: php arrays multidimensional-array dereference


【解决方案1】:

只需像往常一样访问解码后变成数组的json:

foreach($result['SE']['bill_detail']['bill_item'] as $bill_item) {
    // do what you have to do
    echo $bill_item['prod'] . '<br/>';
}

【讨论】:

  • @user2276056 记得点击这篇文章左侧的复选标记,这样其他人就会知道这个问题已经结束/解决了。 meta.stackexchange.com/a/5235
  • 我一定会的!最后一个问题,我的代码在这一行一直返回 false :(
  • @user2276056 是的,它是错误的,绝对是:)。 $bill_item['charge'] 包含浮点值,因此将其与 == "attention" 进行比较没有意义,因此等同于 false
猜你喜欢
  • 2021-02-19
  • 2011-01-28
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
相关资源
最近更新 更多