這裏是我的代碼:JSON字符串PHP數組NULL
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(http://php.net/manual/en/function.json-last-error.php),嘗試確定爲什麼你獲得NULL。我的猜測是你有一些編碼正在進行,當你執行var_dump時會被編碼。 – ernie