Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中从复杂的多维数组中获取值并将其转换为字符串_Php_Arrays_Multidimensional Array - Fatal编程技术网

如何在PHP中从复杂的多维数组中获取值并将其转换为字符串

如何在PHP中从复杂的多维数组中获取值并将其转换为字符串,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个非常复杂的数组,我想为HTML创建获取值 我的数组中有一个目录中的jpg文件列表,但我想要“materialy do plis”目录中的所有文件,这些文件都有包含文件的子目录,有时甚至还有包含更多文件的更多子目录 我想在foreach数组中得到一个生成得很好的URL,最后是目录名和文件 这是我的代码,如果有帮助的话: function pathToArray($path , $separator = '/') { if (($pos = strpos($path, $separa

我有一个非常复杂的数组,我想为HTML创建获取值

我的数组中有一个目录中的jpg文件列表,但我想要“materialy do plis”目录中的所有文件,这些文件都有包含文件的子目录,有时甚至还有包含更多文件的更多子目录

我想在foreach数组中得到一个生成得很好的URL,最后是目录名和文件

这是我的代码,如果有帮助的话:

function pathToArray($path , $separator = '/') {
    if (($pos = strpos($path, $separator)) === false) {
        return array($path);
    }
    return array(substr($path, 0, $pos) => pathToArray(substr($path, $pos + 1)));
}


$dir = APPPATH.'../media/multimedia/obrazy/materialy-do-plis/';
$results = array();
if (is_dir($dir)) {
    $iterator = new RecursiveDirectoryIterator($dir);
    foreach ( new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file ) {
        if ($file->isFile()) {
            $thispath = str_replace('\\', '/', $file);
            $thisfile = utf8_encode($file->getFilename());
            $results = array_merge_recursive($results, pathToArray($thispath));
        }
    }
}
echo "<pre>";
print_r($results);

array_walk($products, function ($value, $key) use ($stores, &$array) {
    $array[$value['store']][] = $value['product'];
});

print_r($array);
函数pathToArray($path,$separator='/')){
if(($pos=strpos($path,$separator))==false){
返回数组($path);
}
返回数组(substr($path,0,$pos)=>pathToArray(substr($path,$pos+1));
}
$dir=APPPATH.“../media/multimedia/obrazy/materialy do plis/”;
$results=array();
if(is_dir($dir)){
$iterator=新的递归目录迭代器($dir);
foreach(新的递归迭代器($iterator,递归迭代器::CHILD_FIRST)作为$file){
如果($file->isFile()){
$thispath=str\u replace(“\\\”、“/”、$file);
$thisfile=utf8_encode($file->getFilename());
$results=array\u merge\u recursive($results,pathToArray($thispath));
}
}
}
回声“;
打印(结果);
array_walk($products,function($value,$key)use($stores,&$array){
$array[$value['store'][]=$value['product'];
});
打印(数组);

基本上我认为,您需要一个递归函数。 此函数可以写入结果数组,该数组可以是类变量,也可以通过引用传入

基本上是这样的(未经测试,但为了获得想法):

class Foo {
    private $results = array();

    function recurse($inputArray, $path) {
        foreach($inputArray as $i => $item) {
            if(is_dir($path."/".$i)) {
                $this->recurse($item, $path."/".$i);
            }elseif(is_file($path."/".$item)){
                $this->results[] = $path."/".$item;
            }
        }
    }
}