php array_push 与关联数组

php array_push with associative array

我正在尝试将文件添加到关联数组。
在搜索时我找到了 "array_push" 函数,但这不适用于关联数组。

然后我发现我应该这样使用:

$myArray[$key] = $value;

所以我尝试了这个:

<?php
/* some SQL code to get the user's instrument */

$dir = "./bladmuziek/$instrument";
$dh = opendir($dir);
$partijen = array();
while (($file = readdir($dh)) !== false) {
    if (strlen($file) >= 3) {
        $file2 = str_replace (" ", "%20", $file);
        list( , $filename) = explode(';', $file);
        list($filename2, ) = explode('.', $filename);
        $partijen[$filename] = $file2;
    }
}

?>

文件格式如下:69845214;部分file.pdf
所以我将文件名“Some file”和 href“69845214;Some%20file.pdf”保存到我的数组“$partijen".

除了我的目录中有一些重复的文件名外,这工作正常。 (由于前面的数字,这在目录中不是问题)

所以我的数组正在覆盖具有相同文件名的文件。

如何在数组的 和 中添加我的信息以便保留所有文件?

只需添加一个索引来生成数组的键。

<?php
/* some SQL code to get the user's instrument */

$dir = "./bladmuziek/$instrument";
$dh = opendir($dir);
$partijen = array();
$i = 0;
while (($file = readdir($dh)) !== false) {
    if (strlen($file) >= 3) {
        $file2 = str_replace (" ", "%20", $file);
        list( , $filename) = explode(';', $file);
        list($filename2, ) = explode('.', $filename);
        $partijen[++$i.'_'.$filename] = $file2;
    }
}

?>