【发布时间】:2012-12-05 00:39:34
【问题描述】:
这是我的代码:
var_dump(json_decode($data['event']->options['meals']['options'][0]['option'], true));
echo '<br />';echo '<br />';
var_dump($data['event']->options['meals']['options'][0]['option']);
echo '<br />';echo '<br />';
var_dump(json_decode('[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]', true));
这是我的输出:
NULL
string(279) "[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]"
array(2) { [0]=> array(2) { ["name"]=> string(16) "Petit Tenderloin" ["description"]=> string(115) "Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. " } [1]=> array(2) { ["name"]=> string(16) "Chicken Piccatta" ["description"]=> string(72) "In lemon caper sauce, served with a timbal of wild rice and vegetables. " } }
为什么当我输入一个字符串字面量时,我得到了正确的数组,但是当我传入一个变量时,我得到了 NULL?我觉得我错过了一些超级简单的东西......
编辑:找出原因 看起来该变量有一个自然不会出现在 HTML 中的换行符。看起来新行字符打破了 json_decode...
除了删除新行之外,任何人都知道解决此问题的方法吗? (如果可以的话,我宁愿保留它们)
【问题讨论】:
-
不确定,但是在您的第一个 var_dump 之后,您可以尝试使用 json_last_error 来尝试确定您为什么会得到 NULL。我的猜测是你正在进行一些编码,当你执行 var_dump 时会被破坏。
标签: php multidimensional-array json