如何从json对象到php数组获取值

如何从json对象到php数组获取值,php,arrays,json,object,Php,Arrays,Json,Object,我正在为一个应用程序编写一个php文件,该应用程序允许用户打印项目报告(使用fpdf库) 其目的是从数据库中获取数据,然后动态构建PDF文件 数据存储在json中 几乎所有的数据都可以,但有些数据我拿不到。 以下是一个例子: 首先,我已经做到了: $Array['roof_coordinates'] = json_decode($Array['roof_coordinates'], false); 要获得“A523500”的编号“nb”,我会这样做: $Array['roof_coordina

我正在为一个应用程序编写一个php文件,该应用程序允许用户打印项目报告(使用fpdf库)

其目的是从数据库中获取数据,然后动态构建PDF文件

数据存储在json中

几乎所有的数据都可以,但有些数据我拿不到。 以下是一个例子: 首先,我已经做到了:

$Array['roof_coordinates'] = json_decode($Array['roof_coordinates'], false);
要获得“A523500”的编号“nb”,我会这样做:

$Array['roof_coordinates']->results->packingList->total->A523500->nb;
我做错了什么

  "roofCoordinates": {

    "results": {

      "packingList": {

        "total": {

          "A523500": {

            "ref": "STRA523500",
            "nb": 16

          },
          "A523120": {

            "ref": "STRA523120",
            "nb": 0

          },
          "A522100": {

            "ref": "STRA522100",
            "nb": 8

          },

        },

      }

    },

}
我尝试将“true”传递给json_decode,以将对象转换为关联数组,但似乎不起作用

任何帮助都会很好!
先谢谢你;)

确保正确访问生成的数据结构,无论是解码到数组还是对象,都应能正常工作

<?php
$json = <<<END
{
  "roofCoordinates": {
    "results": {
      "packingList": {
        "total": {
          "A523500": {
            "ref": "STRA523500",
            "nb": 16
          },
          "A523120": {
            "ref": "STRA523120",
            "nb": 0
          },
          "A522100": {
            "ref": "STRA522100",
            "nb": 8
          }
        }
      }
    }
  }
}
END;

$arrayResults = json_decode($json, true);
$nbFromArray = $arrayResults['roofCoordinates']['results']['packingList']['total']['A523500']['nb'];

$stdClassResults = json_decode($json);
$nbFromStdClass =  $stdClassResults->roofCoordinates->results->packingList->total->A523500->nb;

assert($nbFromArray==16, 'Value should equal 16');
assert($nbFromArray==$nbFromStdClass, 'Values from either json_decode method should be equal');

echo 'From array: '.$nbFromArray.PHP_EOL;
echo 'From stdClass: '.$nbFromStdClass.PHP_EOL;

确保正确访问生成的数据结构,无论是解码到数组还是对象,都应能正常工作

<?php
$json = <<<END
{
  "roofCoordinates": {
    "results": {
      "packingList": {
        "total": {
          "A523500": {
            "ref": "STRA523500",
            "nb": 16
          },
          "A523120": {
            "ref": "STRA523120",
            "nb": 0
          },
          "A522100": {
            "ref": "STRA522100",
            "nb": 8
          }
        }
      }
    }
  }
}
END;

$arrayResults = json_decode($json, true);
$nbFromArray = $arrayResults['roofCoordinates']['results']['packingList']['total']['A523500']['nb'];

$stdClassResults = json_decode($json);
$nbFromStdClass =  $stdClassResults->roofCoordinates->results->packingList->total->A523500->nb;

assert($nbFromArray==16, 'Value should equal 16');
assert($nbFromArray==$nbFromStdClass, 'Values from either json_decode method should be equal');

echo 'From array: '.$nbFromArray.PHP_EOL;
echo 'From stdClass: '.$nbFromStdClass.PHP_EOL;

您是否尝试添加
屋顶坐标
级别-
数组['roof_coordinates']->屋顶坐标->结果->打包列表->总计->A523500->nb但是你有什么问题?您是否收到任何PHP错误?该值与预期值不匹配?var_dump($Array['roof_coordinates']->results->packingList->total->A523500->nb)的输出是什么???您好,谢谢您的回答。输出是0而不是16。我没有任何php错误。只是不是很好的值,如果我尝试使用“ref”,我没有任何输出。您是否尝试添加
rootcoordinates
level-
$Array['roof\u coordinates']->rootcoordinates->results->packingList->total->A523500->nb但是你有什么问题?您是否收到任何PHP错误?该值与预期值不匹配?var_dump($Array['roof_coordinates']->results->packingList->total->A523500->nb)的输出是什么???您好,谢谢您的回答。输出是0而不是16。我没有任何php错误。只是不是很好的值,如果我尝试使用“ref”,我没有任何输出。你是对的,它可以工作。结构(和你的)很好。问题来自db本身。。。事实上,当我试图用console.log()在前端获取json值时,它给了我一些好的值,但是经过计算!问题是,应用程序在计算后还没有保存值。真的很抱歉。再次感谢你;)你是对的,它是有效的。结构(和你的)很好。问题来自db本身。。。事实上,当我试图用console.log()在前端获取json值时,它给了我一些好的值,但是经过计算!问题是,应用程序在计算后还没有保存值。真的很抱歉。再次感谢你;)