我正在学习编程,我似乎无法弄清楚如何追加以下数组。这是一个片段。
<?php
$dir = "./media/";
if($dh = opendir($dir))
{
while(false != ($file = readdir($dh)))
{
if($file != "." && $file != "..")
{
$pathinfo = pathinfo($file);
$pathinfo = $pathinfo['filename'];
$mp3 = "http://localhost/media/" . $file;
$Obj1 = ["title=" => $pathinfo,
"artist=" => "artist1",
"mp3=" => $mp3];
$Obj2 = append($Obj1);
}
}
closedir($dh);
}
?>
我不明白为什么这不起作用。任何帮助表示赞赏。谢谢!
答案 0 :(得分:0)
$dir = "./media/";
// Always initialize your array
$array = array();
if ( $dh = opendir($dir) ) {
while ( false != ( $file = readdir($dh) ) ) {
if ( $file != "." && $file != ".." ) {
$pathinfo = pathinfo($file);
$pathinfo = $pathinfo['filename'];
$mp3 = "http://localhost/media/" . $file;
// Don't do silly things like putting "=" in every key
$thisArray = array(
'title' => $pathinfo,
'artist' => 'artist1',
'mp3' => $mp3
);
$array[] = $thisArray;
// Or $array += $thisArray;
}
}
closedir($dh);
}