简体   繁体   English

我无法将 JSON 更改为 PHP 数组

[英]I can't change JSON into a PHP array

For some strange reason I can't change the following JSON to a PHP array:由于某些奇怪的原因,我无法将以下 JSON 更改为 PHP 数组:

{"sides0":{"name_nl":"Voorkant100","name":"Frontside100","template_overlay":""},"sides1":{"name_nl":"Achterkant100","name":"Backside100","template_overlay":"1"}}

I've validated the json and it's valid.我已经验证了 json 并且它是有效的。

First I post $product['sides'] to my page, containing:首先,我将$product['sides']发布到我的页面,其中包含:

"{\"sides0\":{\"name_nl\":\"Voorkant100\",\"name\":\"Frontside100\",\"template_overlay\":\"\"},\"sides1\":{\"name_nl\":\"Achterkant100\",\"name\":\"Backside100\",\"template_overlay\":\"1\"}}"

Then I use json_decode on it like this:然后我像这样使用 json_decode :

$sidearr = json_decode($product['sides'], true);

If I then do:如果我这样做:

echo '<pre>';
print_r($sidearr);
echo '</pre>';

It prints the first part of my question.它打印了我问题的第一部分。

Now I want to loop over it, but even this test shows nothing:现在我想循环它,但即使这个测试也没有显示:

foreach($sidearr as $side){
    echo 'test';
}

I tried testing if it even is an array with:我尝试测试它是否甚至是一个数组:

echo is_array($sidearr) ? 'Array' : 'not an Array';
echo "\n";

And it shows me that it is not an array.它告诉我它不是一个数组。 Why is that?这是为什么? I always thought using json_decode on a json string and add true inside the function turns it into a PHP array.我一直认为在 json 字符串上使用 json_decode 并在 function 中添加true将其转换为 PHP 数组。

It prints the first part of my question.它打印了我问题的第一部分。

Because $sidearr is a string now, decode it again, you'll get an array.因为$sidearr现在是一个字符串,再次解码它,你会得到一个数组。

$sidearr = json_decode($sidearr, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM