使用1键PHP将2个变量推入数组

使用1键PHP将2个变量推入数组,php,arrays,file,path,key,Php,Arrays,File,Path,Key,我试图将两个变量推入一个数组,但我希望键是相同的 下面的代码是一个搜索功能,它可以搜索一个装满文件的文件夹。 在foreach中,我正在检查名称或名称的一部分是否与搜索词匹配。如果有结果,我将文件名和文件路径放入数组中 protected function search() { $keyword = $this->strKeyword; $foundResults = array(); $dir_iterator = new R

我试图将两个变量推入一个数组,但我希望键是相同的

下面的代码是一个搜索功能,它可以搜索一个装满文件的文件夹。 在foreach中,我正在检查名称或名称的一部分是否与搜索词匹配。如果有结果,我将文件名和文件路径放入数组中

protected function search()
    {

        $keyword = $this->strKeyword;

        $foundResults = array();

        $dir_iterator = new RecursiveDirectoryIterator(TL_ROOT."/tl_files/");
        $iterator = new RecursiveIteratorIterator($dir_iterator,
            RecursiveIteratorIterator::SELF_FIRST);

        foreach ($iterator as $splFile) {
            if ($splFile->getBaseName() == $keyword) {
                array_push($foundResults, $splFile->getBaseName(), $splFile->getPathName());
            }
            elseif(stripos($splFile->getBaseName(), $keyword) >= 3){
                array_push($foundResults, $splFile->getBaseName(), $splFile->getPathName());
            }
        }

        return $foundResults;
    }
当我运行代码时,它会返回以下内容:

[0] => FileName Output 1
[1] => FilePath Output 1
[2] => FileName Output 2
[3] => FilePath Output 2
如您所见,他为文件名和文件路径设置了一个新键

但我想要的是:

[0] => Example
        (
            [fileName] => logo.png
            [pathName] => /tes/blalabaa/ddddd/logo.png
        )
我希望有点清楚,有人能帮我


格里茨我想你需要这样的东西:

$foundResults[] = array(
    'fileName' => $splFile->getBaseName(),
    'pathName' => $splFile->getPathName());

您可以推送包含键值对的数组,而不是值:

array_push($foundResults,
    array(
        'fileName' => $splFile->getBaseName(),
        'filePath' => $splFile->getPathName()
    )
);

Lashane的$foundResults[]解决方案也有相同的功能,但它的阅读更简洁,更具php风格。感谢您的帮助:)我使用了这段代码,效果非常好!非常感谢这是我正在寻找的解决方案+1:)