申し訳ありませんが、より良いタイトルを組み立てることができませんでした。これがfunctions.php内に関数がある問題です
function show_news(){
$id_counter = 1;
$json_news = array(
"id" => 0,
"title" => ""
);
$json_o = json_decode(file_get_contents(JSON_DATA_FOLDER.'news.json'));
foreach ($json_o as $id => $news_category)
{
echo '<h2>'.$id.'<h2>';
foreach ($news_category as $news)
{
if(IsNullOrEmptyString($news->id)){$json_news['id'] = $id_counter; $id_counter++;}
else{$json_news['id']=$news->id;}
if(!IsNullOrEmptyString($news->title)){$json_news['title']=$news->title;}
var_dump($json_news);
echo "<br/>-------<br/>";
include('news-layout.php');
}
}
}
私はjsonファイルを読んでおり、要素ごとにその値を配列に割り当てています。次に、「news-layout.php」を含めます。テストの目的で、これらの3行のコードだけを「news-layout.php」内に保持しました。
<?php
global $json_news;
var_dump($json_news);
echo"<br/>=======================<hr/>";
?>
そのため、関数内と含まれているページでvar_dumpを実行しています。しかし、私は奇妙な結果を得ています。含まれているページのvar_dump($ json_news)がループの最初の反復でNULLを示すことを除いて、すべてが正常に機能します。これが出力です
todays_specials
array(2) { ["id"]=> int(1) ["title"]=> string(26) "Okie Since I have to do it" }
-------
NULL
=======================
array(2) { ["id"]=> int(2) ["title"]=> string(16) "Vegetable Samosa" }
-------
array(2) { ["id"]=> int(2) ["title"]=> string(16) "Vegetable Samosa" }
=======================
array(2) { ["id"]=> int(3) ["title"]=> string(16) "Vegetable Pakora" }
-------
array(2) { ["id"]=> int(3) ["title"]=> string(16) "Vegetable Pakora" }
=======================
あなたはそこに奇妙なNULLが来るのを見ることができます。誰かが何が起こっているのか、それを修正する方法を説明できますか?