2017-06-21 85 views
0

我有非常复杂的数组,我想为HTML创建获取值。如何从PHP中的复杂多维数组中获取值并将它们转换为字符串

我在我的数组中有目录中的jpg文件列表,但我希望所有来自'materialy-do-plis'目录的文件都有文件的子目录,有时甚至更多的子目录有更多的文件。

我想在foreach数组中获得一个很好生成的URL,并在目录名和文件末尾。

这是我的数组怎么样子:https://paste.ofcode.org/vE7qBXvGZNDGMSenF9ijST(超长)

这是我的代码得到它,如果这能帮助:

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); 

回答

0

基本上,我想,你需要一个递归函数。 该函数可以写入结果数组,该结果数组可能是一个类变量或通过引用传入。

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

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; 
      } 
     } 
    } 
} 
+0

我会尝试一下,谢谢。 – Aksebkit

相关问题