3つのディレクトリからすべてのmyファイル(.pdf)を読み込み、jsonファイルを作成するPHPスクリプトを作成したいとします。リストファイルphp from - jsonファイルを作成
私はこの
$dir_2016 = "./HCL/2016";
$dir_2015 = "./HCL/2015";
$dir_2014 = "./HCL/2014";
$files_2016 = array();
$files_2015 = array();
$files_2014 = array();
$json_file = array(
"2016" => $files_2016,
"2015" => $files_2015,
"2014" => $files_2014
);
if(is_dir($dir_2016) and is_dir($dir_2015) and is_dir($dir_2014))
{
// 2016
if(is_dir($dir_2016))
{
if($dh = opendir($dir_2016))
{
while(($file = readdir($dh)) != false)
{
if($file == "." or $file == ".."){
} else {
$files_2016[] = $file; // Add the file to the array
}
}
}
}
// 2015
if(is_dir($dir_2015))
{
if($dh = opendir($dir_2015))
{
while(($file = readdir($dh)) != false)
{
if($file == "." or $file == ".."){
} else {
$files_2015[] = $file; // Add the file to the array
}
}
}
}
// 2014
if(is_dir($dir_2014))
{
if($dh = opendir($dir_2014))
{
while(($file = readdir($dh)) != false)
{
if($file == "." or $file == ".."){
} else {
$files_2014[] = $file; // Add the file to the array
}
}
}
}
echo json_encode($json_file);
}
を試みるが、出力は次のようになります。
{"2016":[],"2015":[],"2014":[]}
files_2014 []、files_2015 []、files_2016 []は空です。
私は間違っていますか?
$ json_fileの定義を下に移動します。 $ files_2016を割り当てた時点で、その変数は*空です*。あなたはそれを挿入する前にそれを記入する必要があります。 –
これを '$ json_file = array( " 2016 "=> $ files_2016," 2015 "=> $ files_2015、 " 2014 "=> $ files_2014 )に移動します。 'あなたのすべてのループの後に – nospor
あなたのコードをリファクタリングすることを考えましたか?それは本当に繰り返しです。 https://3v4l.org/DqJAfのようなものも同様に機能します。 – Yoshi