我无法通过多维数组正常排便
I cant loop properly through multdimensional array
我的文件夹和文件有一个多维数组。它看起来像这样:
$arr = array(
0 => 'file.php',
1 => 'file2.php',
'folder1' => array(
'subfolder1' => array(
0 => 'subfile.php',
1 => 'subfile2.php',
2 => 'subfile3.php'
)
),
'folder2' => array(
0 => 'image.jpg',
1 => 'image2.jpg',
'subfolder2' => array(
0 => 'subimage3.jpg',
1 => 'subimage4.jpg'
),
'subfolder3' => array(
0 => 'subfile4.php',
1 => 'subfile5.php'
),
),
);
我正在编写一个函数来遍历此数组以获取简单的文件列表及其路径。这是功能:
function multiarrToList($arr,$list = false,$path = false){
if(!is_array($list)){
$list = array();
}
if(!is_array($path)){
$path[] = '';
}
foreach($arr as $key => $value){
if(is_array($value)){
$path[] = $key;
$list = multiarrToList($arr[$key],$list,$path);
} else {
$list[] = array(
'filename' => $value,
'path' => $path
);
}
}
return $list;
}
我卡住了,因为我正在正确循环以获取每个文件名,但路径无法正常工作。您可以在第 5 项的结果中看到它,依此类推。有重复的文件夹,不应该在他的路径中。结果:
Array
(
[0] => Array
(
[filename] => file.php
[path] => Array
(
[0] =>
)
)
[1] => Array
(
[filename] => file2.php
[path] => Array
(
[0] =>
)
)
[2] => Array
(
[filename] => subfile.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => subfolder1
)
)
[3] => Array
(
[filename] => subfile2.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => subfolder1
)
)
[4] => Array
(
[filename] => subfile3.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => subfolder1
)
)
[5] => Array
(
[filename] => image.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
)
)
[6] => Array
(
[filename] => image2.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
)
)
[7] => Array
(
[filename] => subimage3.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
)
)
[8] => Array
(
[filename] => subimage4.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
)
)
[9] => Array
(
[filename] => subfile4.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
[4] => subfolder3
)
)
[10] => Array
(
[filename] => subfile5.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
[4] => subfolder3
)
)
)
感谢任何想法。
你已经明白了其中的要旨。使这项工作的几个变化如下:
将 function multiarrToList($arr, $list = false, $path = false){
更改为 function multiarrToList($arr, $list = [], $path = []){
。我们将它们设置为默认的空数组。这可以帮助您完全消除对下一个 if
检查的需要。
$path[] = $key;
是重复的原因。这是因为对于每个新的后续 is_array()
检查,总是包括以前的 $key
。要纠正此问题,请制作数组的新副本,将当前 $key
添加到其中,并将数组的这个新副本传递给它的下一个子调用。
$list = multiarrToList($arr[$key],$list,$path);
- 在此,我建议用子递归结果覆盖当前 $list
的 array_merge()
,而不是覆盖 $list
。
multiarrToList($arr[$key],$list,$path);
不用再传$list
了。只需传递一个空数组,让它只 return 只有递归子层次结构结果 才能在父级顶层合并。
片段:
<?php
function multiarrToList($arr, $list = [], $path = []){
foreach($arr as $key => $value){
if(is_array($value)){
$new_path = $path;
$new_path[] = $key;
$list = array_merge($list, multiarrToList($value, [], $new_path));
} else {
$list[] = [
'filename' => $value,
'path' => $path
];
}
}
return $list;
}
我的文件夹和文件有一个多维数组。它看起来像这样:
$arr = array(
0 => 'file.php',
1 => 'file2.php',
'folder1' => array(
'subfolder1' => array(
0 => 'subfile.php',
1 => 'subfile2.php',
2 => 'subfile3.php'
)
),
'folder2' => array(
0 => 'image.jpg',
1 => 'image2.jpg',
'subfolder2' => array(
0 => 'subimage3.jpg',
1 => 'subimage4.jpg'
),
'subfolder3' => array(
0 => 'subfile4.php',
1 => 'subfile5.php'
),
),
);
我正在编写一个函数来遍历此数组以获取简单的文件列表及其路径。这是功能:
function multiarrToList($arr,$list = false,$path = false){
if(!is_array($list)){
$list = array();
}
if(!is_array($path)){
$path[] = '';
}
foreach($arr as $key => $value){
if(is_array($value)){
$path[] = $key;
$list = multiarrToList($arr[$key],$list,$path);
} else {
$list[] = array(
'filename' => $value,
'path' => $path
);
}
}
return $list;
}
我卡住了,因为我正在正确循环以获取每个文件名,但路径无法正常工作。您可以在第 5 项的结果中看到它,依此类推。有重复的文件夹,不应该在他的路径中。结果:
Array
(
[0] => Array
(
[filename] => file.php
[path] => Array
(
[0] =>
)
)
[1] => Array
(
[filename] => file2.php
[path] => Array
(
[0] =>
)
)
[2] => Array
(
[filename] => subfile.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => subfolder1
)
)
[3] => Array
(
[filename] => subfile2.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => subfolder1
)
)
[4] => Array
(
[filename] => subfile3.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => subfolder1
)
)
[5] => Array
(
[filename] => image.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
)
)
[6] => Array
(
[filename] => image2.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
)
)
[7] => Array
(
[filename] => subimage3.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
)
)
[8] => Array
(
[filename] => subimage4.jpg
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
)
)
[9] => Array
(
[filename] => subfile4.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
[4] => subfolder3
)
)
[10] => Array
(
[filename] => subfile5.php
[path] => Array
(
[0] =>
[1] => folder1
[2] => folder2
[3] => subfolder2
[4] => subfolder3
)
)
)
感谢任何想法。
你已经明白了其中的要旨。使这项工作的几个变化如下:
将
function multiarrToList($arr, $list = false, $path = false){
更改为function multiarrToList($arr, $list = [], $path = []){
。我们将它们设置为默认的空数组。这可以帮助您完全消除对下一个if
检查的需要。$path[] = $key;
是重复的原因。这是因为对于每个新的后续is_array()
检查,总是包括以前的$key
。要纠正此问题,请制作数组的新副本,将当前$key
添加到其中,并将数组的这个新副本传递给它的下一个子调用。$list = multiarrToList($arr[$key],$list,$path);
- 在此,我建议用子递归结果覆盖当前$list
的array_merge()
,而不是覆盖$list
。multiarrToList($arr[$key],$list,$path);
不用再传$list
了。只需传递一个空数组,让它只 return 只有递归子层次结构结果 才能在父级顶层合并。
片段:
<?php
function multiarrToList($arr, $list = [], $path = []){
foreach($arr as $key => $value){
if(is_array($value)){
$new_path = $path;
$new_path[] = $key;
$list = array_merge($list, multiarrToList($value, [], $new_path));
} else {
$list[] = [
'filename' => $value,
'path' => $path
];
}
}
return $list;
}