外部ファイル test.jsonに
JSON
1[{"id":"1"}] 2```上記のような内容があり、 3それをdisplay.phpの方で利用したいのですがどうすればいいでしょうか。 4 5```PHP 6//display.php 7 8//このコードでは動作しませんでした。 9$test = file_get_contents('test.json'); 10print_r(json_decode($test));
よろしくお願いいたします。
追記
コード1:
PHP
1<?php 2ini_set('display_errors', 1); 3error_reporting(E_ALL); 4foreach(json_decode(file_get_contents('test.json'), true) as $key => $val) { 5 echo $key.' => '.var_export($val, true).PHP_EOL; 6} 7?>
結果1:
Warning: Invalid argument supplied for foreach() in /var/www/html/test2.php on line 5
やはり文字列がJsonとして、認識されないです…
コード2:
PHP
1$test = file_get_contents('test.json'); 2var_dump($test);
結果2:
string(17) "[{"id":"1"}] "
上記結果が返ってきています
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/18 07:09
2017/07/18 07:17