Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP向json_decode()函数提供PHP数组?_Php_Arrays_Json - Fatal编程技术网

PHP向json_decode()函数提供PHP数组?

PHP向json_decode()函数提供PHP数组?,php,arrays,json,Php,Arrays,Json,我有一个合适的.json物理文件,我通过解析从PHP读取它 假设sales.json包含: { "custid" : "7761", "items" : [ { "itemcode" : "A11231G", "suppliers" : [ { "id" : "s10001", "name" : "Be

我有一个合适的
.json
物理文件,我通过解析从PHP读取它

假设
sales.json
包含:

{
    "custid" : "7761", 
    "items" : [
        {
            "itemcode"  : "A11231G",
            "suppliers" : [
                {
                    "id" : "s10001",
                    "name" : "Benny & John",
                },
                            {
                    "id" : "s10004",
                    "name" : "Colorado Dimension",
                }
            ]
        }
    ]
}
然后我从PHP中使用它:

$sales = json_decode( file_get_contents("store/sales.json"), true );
没有问题,
$sales
已经成为一个数组,可以了

现在出于某种原因,我想为
json\u decode()
函数提供一个PHP数组(而不是
.json
物理文件)


我知道这是一种愚蠢的方式,我实际上在做转换,
Array
->
json
->
Array
,最后是
Array
Array


但不管出于什么原因

  • 即使我有一个结构正确的PHP数组(),如果我使用
    json\u-encode($phpArray)
    ,然后作为
    json\u-decode(json\u-encode($phpArray),true)
    ,它会给出与我从
    json\u-decode(“sales.json”)
    文件中得到的确切对象吗
  • (或者)如何使用我拥有的PHP
    Array()
    对象为
    json\u decode
    函数提供数据

实际上,json_decode返回的是STD对象,而不是数组。要从JSON字符串获取数组,您需要使用
JSON\u decode($string,true)

好吧,看看他的代码:
JSON\u decode(file\u get\u contents(“settings/config.JSON”),true你好Wordpress开发者,我不是问从JSON获取数组。我问的是如何转换
PHP数组->JSON编码->JSON解码->PHP数组
。您可以放心地假设,对现有数组进行JSON_编码,然后对结果字符串进行JSON_解码,将生成相同的数组,但有一个例外:输入数组中的所有元素都必须可序列化。听起来像是XY问题。你到底想达到什么目的?表示问题中发布的json无效。您可以使用serialize()将数组存储在一个文件中,然后取消序列化并对其进行json编码,为什么不直接更改数组?@xdazz(不是真的,但您可以想象)包含
$sales=json\u decode(file\u get\u contents(“store/sales.json”),true)的php文件(设计用于读取json文件)不在您的控制范围内。意味着,在某些情况下,您无法更改写入其中的函数。然后我必须只向函数提供JSON。