本文介绍了在 PHP 中迭代复杂的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以在 PHP 中迭代此结构的关联数组:

Is there an easy way to iterate over an associative array of this structure in PHP:

数组 $searches 有一个编号索引,有 4 到 5 个关联部分.所以我不仅需要通过 $searches[n] 遍历 $searches[0],还需要 $searches[0]["part0"]$searches[n]["partn"].困难的部分是不同的索引有不同数量的部分(有些可能缺少一两个).

The array $searches has a numbered index, with between 4 and 5 associative parts. So I not only need to iterate over $searches[0] through $searches[n], but also $searches[0]["part0"] through $searches[n]["partn"]. The hard part is that different indexes have different numbers of parts (some might be missing one or two).

有没有想过以一种漂亮、整洁且易于理解的方式来做这件事?

Thoughts on doing this in a way that's nice, neat, and understandable?

推荐答案

嵌套两个 foreach 循环:

Nest two foreach loops:

foreach ($array as $i => $values) {
    print "$i {\n";
    foreach ($values as $key => $value) {
        print "    $key => $value\n";
    }
    print "}\n";
}

这篇关于在 PHP 中迭代复杂的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 09:17