在for循环php中将数组合并到一个主数组中

时间:2014-05-03 05:45:48

标签: php

我有一个创建数组的for循环,如何在一个主数组中合并所有数组

for ($i = 1; $i <= 10; $i++) 
    {
        $dirname =   substr( $ring['ringPicDir'] ,0, -4 );
        $lastpart = basename( $dirname , ".zip");
        $abspath = substr( $dirname ,strpos( $dirname, "com/")+4 );
        $direcotypath = "../".$abspath."/".$lastpart;
        $files = glob($direcotypath."/*.*");
        foreach ($files as $key => &$value) 
            {
                $value = str_replace( "..", $url, $value);
            }

        $mainFiles = $mainFiles + $files;// Something like this??
    }

1 个答案:

答案 0 :(得分:1)

好吧,你可以试试这段代码:

$mainFiles = array();
for ($i = 1; $i <= 10; $i++) {
    $dirname =   substr( $ring['ringPicDir'] ,0, -4 );
    $lastpart = basename( $dirname , ".zip");
    $abspath = substr( $dirname ,strpos( $dirname, "com/")+4 );
    $direcotypath = "../".$abspath."/".$lastpart;
    $files = glob($direcotypath."/*.*");
    foreach ($files as $key => &$value) {
        $value = str_replace( "..", $url, $value);
    }
    $mainFiles[] = $files;
}

所以用$ mainFiles显示文件数组,

echo $mainFiles[0]['key name here'];

另请查看Multidimensional Arrays