所以我有这个脚本,我试图在php中创建一个filelist.js文件,这里是代码:
//CREATE fileslist.js
$thumbssize = "1";
$moviesize= "1";
$thumbarray = array_slice(scandir($tpath), 2);
echo print_r($thumbarray) . "<br/>";
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$script="
fileList = {};
fileList[\"thumbs\"] = {};
fileList[\"movies\"] = {};
fileList[\"title\"]=\"{$foldername}\";";
foreach($thumbarray as $item) {
print "fileList[\"thumbs\"][1]=["$item['0']","$item['2']","$item['3']","$item['4']","$item['5']","$item['6']"];";
}
$script2 = "
fileList[\"thumbs\"][\"size\"]={$thumbssize};
fileList[\"thumbs\"][\"size\"]={$moviesize};";
//$script = preg_replace('/\s\s+/', ' ', $script);
$scriptName= $_SERVER['DOCUMENT_ROOT'] . "/{$foldername}/assets/js/filelist.js";
file_put_contents($scriptName, trim($script . $script2) . "\n", FILE_APPEND);
echo "Generating filelist.js</br>";
}
这是数组的示例输出:
Array ( [0] => 863_example_r003.mp4 [1] => 863_example_r004.mp4 [2] => 863_example_r005.mp4 [3] => 863_example_r006.mp4 [4] => 863_example_r007.mp4 ) 1
这是现在的filelist.js输出:
fileList = {};
fileList["thumbs"] = {};
fileList["movies"] = {};
fileList["title"]="test7";
fileList["thumbs"]["size"]=1;
fileList["thumbs"]["size"]=1;
所以直到这一点它做我想要的,除了我无法弄清楚如何使foreach循环生成这种类型的输出:
fileList = {};
fileList["thumbs"] = {};
fileList["movies"] = {};
fileList["title"]="863-example";
fileList["thumbs"][1]=["863_example_r048.mp4","863_example_r049.mp4","863_example_r050.mp4","863_example_r051.mp4","863_example_r052.mp4","863_example_r053.mp4"];
fileList["thumbs"][2]=["863_example_r054.mp4","863_example_r055.mp4","863_example_r056.mp4","863_example_r057.mp4","863_example_r058.mp4","863_example_r059.mp4"];
fileList["thumbs"][3]=["863_example_r060.mp4","863_example_r061.mp4","863_example_r062.mp4","863_example_r003.mp4","863_example_r063.mp4","863_example_r064.mp4"];
fileList["thumbs"][4]=["863_example_r065.mp4","863_example_r004.mp4","863_example_r067.mp4","863_example_r068.mp4","863_example_r069.mp4","863_example_r070.mp4"];
fileList["thumbs"][5]=["863_example_r005.mp4","863_example_r071.mp4","863_example_r072.mp4","863_example_r073.mp4","863_example_r074.mp4","863_example_r006.mp4"];
fileList["thumbs"][6]=["863_example_r075.mp4","863_example_r076.mp4","863_example_r077.mp4","863_example_r078.mp4","863_example_r007.mp4","863_example_r079.mp4"];
fileList["thumbs"][7]=["863_example_r080.mp4","863_example_r081.mp4","863_example_r082.mp4","863_example_r008.mp4","863_example_r083.mp4","863_example_r084.mp4"];
fileList["thumbs"][8]=["863_example_r085.mp4","863_example_r086.mp4","863_example_r009.mp4","863_example_r087.mp4","863_example_r088.mp4","863_example_r089.mp4"];
[1],[2],[3]是指页码,每页需要6个项目,如上例所示
答案 0 :(得分:1)
感谢您的反馈。就在一分钟前,我对代码进行了改进,并在我的localhost中进行了测试。以下是提供结果的更改代码:
$script1 = "";
$i = 0;
$j = 0;
$inrow = 6; // files in row
foreach($thumbarray as $item => $itemv) {
// beginning of the row
if($j % $inrow == 0){
$script1 .= "\nfileList[\"thumbs\"][".($i+1)."]=[";
}
$script1 .= "\"" . $itemv . "\","; // add filename "863_example_r048.mp4",
// end of the row
if(($j + $inrow + 1) % $inrow == 0){
// Delete comma at end of row
if(substr($script1,-1) == ","){
$script1 = substr($script1, 0, strlen($script1) - 1);
}
$script1 .= "];";
}
if($j % $inrow == 0){
$i++;
}
$j++;
}
以下是测试结果:
fileList["thumbs"][1]=[".AndroidStudio1.3",".ICEauthority",".PlayOnLinux",".Skype",".Xauthority",".adobe"];
fileList["thumbs"][2]=[".android",".audacity-data",".bash_history",".bashrc",".bibletime",".cache"];
fileList["thumbs"][3]=[".claws-mail",".config",".dbus",".directory",".dmrc",".dosbox"];
fileList["thumbs"][4]=[".emacs",".esd_auth",".fltk",".fontconfig",".fonts",".fonts.conf"];
fileList["thumbs"][5]=[".frozen-bubble",".gimp-2.8",".gnome",".gnome2",".gnome2_private",".gnupg"];
fileList["thumbs"][6]=[".gstreamer-0.10",".gtkrc-2.0",".gvfs",".icewm",".inputrc",".ivy2"];
fileList["thumbs"][7]=[".java",".jsword",".kde",".kde4",".kismet",".local"];
fileList["thumbs"][8]=[".macromedia",".mozilla",".mplayer",".mtpaint",".opera",".pki"];
fileList["thumbs"][9]=[".profile",".putty",".pyhistory",".qt",".remmina",".sbt"];
fileList["thumbs"][10]=[".skel",".ssh",".streamtuner",".sword",".themes",".thumbnails"];
fileList["thumbs"][11]=[".thunderbird",".vnc",".wine",".xboardrc",".xdg_menu_cache",".xim.template"];
fileList["thumbs"][12]=[".xinitrc.template",".xiphos",".xscreensaver",".xsession-errors",".xsession-errors-192.168.1.101:1",".xsession-errors-192.168.1.103:1"];
fileList["thumbs"][13]=[".xsession-errors-:0",".xsession-errors-:1",".y2log",".y2usersettings","20150901_134031.jpg","281436_wpmu-dev-dashboard-3.5.3.zip"];
fileList["thumbs"][14]=["Android","AndroidStudioProjects","Bible Analyzer User Modules","Calibre Library","Desktop","Documents"];
fileList["thumbs"][15]=["Downloads","Java","Kismet-20141124-23-54-29-1.alert","Kismet-20141124-23-54-29-1.gpsxml","Kismet-20141124-23-54-29-1.nettxt","Kismet-20141124-23-54-29-1.netxml"];
fileList["thumbs"][16]=["Kismet-20141124-23-54-29-1.pcapdump","Movies","Music","Phpgtk","Pictures","PlayOnLinux's virtual drives"];
fileList["thumbs"][17]=["Public","Templates","Videos","VirtualBox VMs","WPMUDEV_PLUGINS.zip","battery"];
fileList["thumbs"][18]=["bibleanalyzer","bibledesktop-2.0-beta","bin","ca.key","deklaracia.jpg","deklaracia2.jpg"];
fileList["thumbs"][19]=["dfc","http:⁄⁄www.biblestudytools.com⁄niv⁄.desktop","konq","projects","public_html","scala"];
fileList["thumbs"][20]=["somefile","somefile.txt","viber","wget-log","wp-cron.php?import_key=d&import_id=20&action=processing","Запис0003.amr"];
答案 1 :(得分:0)
您需要更改以下部分
$index = 1;
foreach($thumbarray as $item) {
$script .= "fileList[\"thumbs\"][$index]= ".json_encode($item).";" ;
$index++;
}
答案 2 :(得分:0)
您需要两个索引。每次$ i在$ inrow中被删除时,$ i会增加,其余为0.如果你想要6行文件,那么$ i应该比$ j增加6倍,每次增加$ j。这种方式$ i会在$ j为6,12,18,24等时改变。你需要玩一点代码......
$script1 = "";
$i = 0;
$j = 0
$inrow = 6; // files in row
foreach($thumbarray as $item => $itemv) {
// beginning of the row
if($j % $inrow == 0){
$script1 .= "fileList[\"thumbs\"][".($i+1)."]=[";
}
$script1 .= "\"" . $itemv . "\","; // add filename "863_example_r048.mp4",
// end of the row
if($j % $inrow == 0){
// Delete comma at end of row
if(substr($script1,-1) == ","){
$script1 = substr($script1, 0, strlen($script1) - 1);
}
$script1 .= "]";
$i++;
}
$j++;
}