所以我试图遍历一个如下所示的数组。但是,我从文件系统上的文件中派生出这个数组,因此创建的目录越多,这个数组就会有更多的维度。
我使用foreach尝试了几种不同的东西,但我似乎无法动态地工作。我在PHP 5.5的列表函数方面也取得了一些成功,但是在它停止帮助之前,这只能让我在阵列结构中到目前为止。
有什么想法吗?
Array
(
[First Response Manual] => Array
(
[0] => Facilities Maintenance-Locksmith Standby Schedule.pdf
[1] => First Response Manual 2011.pdf
[2] => First Response Manual Fall 2010.pdf
[3] => ORL_Facilities_Maint_and_ Lock_schedule_12 22 2011---03 15 12.pdf
)
[Phone Guides] => Array
(
[0] => ORL_Facilities_Maint_and_ Lock_schedule_01 21 2011---04 14 11.pdf
[1] => ORL_Facilities_Maint_and_ Lock_schedule_08 06 2010---10 28 10.pdf
[2] => Phone Guide.pdf
)
[0] => RA Job Manual.pdf
[RA Manual] => Array
(
[0] => Foreword.doc
[1] => RA Job Manual - Updated August 2012.pdf
[2] => RA Job Manual - Updated June 2011.doc
)
[Section 1 - Resources] => Array
(
[0] => 12 - Very Important Phone Numbers.doc
[1] => AA List- 2009.doc
[2] => Library Handout.pdf
[3] => Mental Health Resources.docx
[4] => ORL Resource Library.xls
[5] => RA Parent Tips.docx
[6] => event calendar brochure- LIT.docx
[7] => iServiceDesk Procedures.doc
)
[Section 2 - Emergency Procedures] => Array
(
[0] => Emergency Situations.doc
[1] => Thumbs.db
[2] => bomb threat check list.jpg
[3] => emergency quick ref sheet.pub
[4] => map to Fort Hospital.jpg
[5] => map to Fort Hospital.pdf
)
[Section 3 - Helping Skills] => Array
(
[Academics] => Array
(
[0] => Academic Advising Primer for RA Staff.docx
[1] => Academic Survival Skills.doc
[2] => Academics.doc
[3] => Developmental Issues.doc
)
[Communication] => Array
(
[0] => American Idioms.doc
[1] => Aspergers 101.ppsx
[2] => Conflict Mediation.doc
[3] => First Year Students.doc
[4] => Helping Skills.doc
[5] => International Students.doc
[6] => LGBT Ally.doc
[7] => LGBT Students.doc
[8] => Peer Counseling.doc
[9] => Referrals.doc
[10] => Resolving Roommate Conflicts.doc
)
[Health and Safety] => Array
(
[0] => AIDS and HIV.doc
[1] => Alcohol 2.doc
[2] => Alcohol.doc
[3] => Dating Violence.doc
[4] => Depression.doc
[5] => Drug Abuse.doc
[6] => Eating Disorders.doc
[7] => Evaluating Eating and Exercise Habits.pdf
[8] => Grief.doc
[9] => Males With Eating Disorders.doc
[10] => Meningitis.doc
[11] => National Eating Disorder Organizations.doc
[12] => STDs (2).doc
[13] => STDs.doc
[14] => Sexual Assault Acquaintance.doc
[15] => Sexual Assault.doc
[16] => Sexual Harassment.doc
[17] => Stress.doc
[18] => Substance Abuse Dependence.doc
[19] => Suicide.doc
[20] => stalking brochure.pdf
)
)
)
答案 0 :(得分:0)
由于你有PHP 5.5,你可以使用生成器:
function recursiveIterate($a)
{
if (is_array($a) || $a instanceof \Traversable) {
foreach ($a as $entry) {
foreach (recursiveIterate($entry) as $value) yield $value;
}
} else yield $a;
}
$myBigArray = array(/* stuff */);
foreach (recursiveIterate($myBigArray) as $value) {
//do something with $value
}
答案 1 :(得分:0)
SPL包含各种各样的迭代器。例如,您可以使用RecursiveArrayIterator
:
$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iter as $k => $v
if (is_array($v)) {
print "$k\n";
} else {
print "$v\n";
}
}
但是你提到这是来自文件系统,所以为什么不使用RecursiveDirectoryIterator
来处理:
$dirIter = new RecursiveDirectoryIterator('/path/to/dir', FilesystemIterator::SKIP_DOTS|FilesystemIterator::CURRENT_AS_FILEINFO|FilesystemIterator::KEY_AS_PATHNAME);
$iter = new RecursiveIteratorIterator($dirIter, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iter as $k => $v) {
$line = "%s: %s\n";
printf($line, 'pathname', $v->getPathname());
printf($line, 'filename', $v->getFilename());
printf($line, 'path', $v->getPath());
}