PHP数组问题,array_merge()没有解决问题
如何解决数组问题?
例外结果:
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
[2] => 2011/02/21
)
我现在得到的失败结果:
Array
(
[0] => 2011/03/13
)
Array
(
[0] => 2011/03/14
)
Array
(
[0] => 2011/02/21
)
PHP 代码:
<?php
function get_dir_iterative($dir='.',$exclude=array('cgi-bin','.','..')){
$exclude=array_flip($exclude);
if(!is_dir($dir)){return;}
$dh=opendir($dir);
if(!$dh){return;}
$stack=array($dh);
$level=0;
while(count($stack)){
if(false!==($file=readdir($stack[0]))){
if(!isset($exclude[$file])){
if(is_dir("$dir/$file")){
$dh=opendir("$file/$dir");
if($dh){
$d=$file;
array_unshift($stack,$dh);
++$level;
}
}else{
if(isset($d)&&$level>0){
$mod=date('Y/m/d',filemtime("$d/$file"));
$ds="$d/";
}else{
$mod=date('Y/m/d',filemtime($file));
$ds='';
}
$array=array($mod);
//$b=array_merge($array); // it doesn't solve the problem
print_r($array);
}
}
}else{
closedir(array_shift($stack));
--$level;
}
}
}
get_dir_iterative();
?>
更新:
将 $array=array($mod);
替换为 $array[]=$mod;
时,不会返回异常结果。
Array
(
[0] => 2011/03/13
)
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
)
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
[2] => 2011/02/21
)
How to solve array problem?
Excepted result:
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
[2] => 2011/02/21
)
Failed result that I get now:
Array
(
[0] => 2011/03/13
)
Array
(
[0] => 2011/03/14
)
Array
(
[0] => 2011/02/21
)
PHP code:
<?php
function get_dir_iterative($dir='.',$exclude=array('cgi-bin','.','..')){
$exclude=array_flip($exclude);
if(!is_dir($dir)){return;}
$dh=opendir($dir);
if(!$dh){return;}
$stack=array($dh);
$level=0;
while(count($stack)){
if(false!==($file=readdir($stack[0]))){
if(!isset($exclude[$file])){
if(is_dir("$dir/$file")){
$dh=opendir("$file/$dir");
if($dh){
$d=$file;
array_unshift($stack,$dh);
++$level;
}
}else{
if(isset($d)&&$level>0){
$mod=date('Y/m/d',filemtime("$d/$file"));
$ds="$d/";
}else{
$mod=date('Y/m/d',filemtime($file));
$ds='';
}
$array=array($mod);
//$b=array_merge($array); // it doesn't solve the problem
print_r($array);
}
}
}else{
closedir(array_shift($stack));
--$level;
}
}
}
get_dir_iterative();
?>
Update:
On replacing $array=array($mod);
with $array[]=$mod;
does not return excepted result.
Array
(
[0] => 2011/03/13
)
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
)
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
[2] => 2011/02/21
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
array_merge() 需要几个参数,即要合并的数组,在您的情况下应该是:
请注意,您可以简单地向全局数组(第二个示例)添加一个新条目来添加值。 Array_merge 消耗的内存超出了它的需要。
array_merge() takes several parameters, the arrays that you want to merge, in your situation it should be:
Note that you can simply add a new entry to your global array (second example) to add a value. Array_merge eats more memory than it needs.
其一,array_merge 至少需要两个数组才能完成任务。您只提供一个,因此没有任何内容可以合并。您的“实时”版本每次都会创建一个新数组,因此这就是您获得“错误”输出的原因。为什么不简单地
在每次迭代时将文件的 mtime 附加到数组中呢?您可能还想存储文件的详细信息,这样您就知道 mtime 来自哪里,因此
可能更有用。
For one, array_merge requires at least two arrays to do its thing. You're only providing one, so there's nothing to merge with. Your "live" version creates a new array each time, so that's why you're getting the 'bad' output. Why not simply do
which'll append the file's mtime to the array on each iteration? You might want to store the file's details as well, so you know where the mtime's coming from, so
might be of more use.
您可以通过将此代码替换为以下代码:
$array=array($mod);
来实现此目的:$array[]=$mod;
。You can achieve this by replacing this code:
$array=array($mod);
with this code:$array[]=$mod;
.尝试用
$b[] = $array
替换注释掉的行Try replacing the commented out line with
$b[] = $array