在 PHP 中迭代复杂的关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:13:15  来源:igfitidea点击:

Iterating over a complex Associative Array in PHP

phparraysassociative-array

提问by Thomas Owens

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

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

The array $searcheshas 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).

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

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

有没有想过以一种很好的、​​整洁的和可以理解的方式来做这件事?

回答by Konrad Rudolph

Nest two foreachloops:

嵌套两个foreach循环

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

回答by Gordon

I know it's question necromancy, but iterating over Multidimensional arrays is easy with Spl Iterators

我知道这是死灵法的问题,但是使用 Spl Iterators 很容易迭代多维数组

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

foreach($iterator as $key=>$value) {
    echo $key.' -- '.$value.'<br />';
}

See

回答by Milan Babu?kov

Looks like a good place for a recursive function, esp. if you'll have more than two levels of depth.

看起来是递归函数的好地方,尤其是。如果您有两个以上的深度级别。

function doSomething(&$complex_array)
{
    foreach ($complex_array as $n => $v)
    {
        if (is_array($v))
            doSomething($v);
        else
            do whatever you want to do with a single node
    }
}

回答by Re0sless

You should be able to use a nested foreach statment

您应该能够使用嵌套的 foreach 语句

from the php manual

来自php 手册

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

回答by Mark Biek

Can you just loop over all of the "part[n]" items and use isset to see if they actually exist or not?

您可以循环遍历所有“part[n]”项并使用 isset 来查看它们是否确实存在?

回答by Ross

I'm really not sure what you mean here - surely a pair of foreach loops does what you need?

我真的不确定你在这里的意思 - 一对 foreach 循环肯定能满足你的需求?

foreach($array as $id => $assoc)
{
    foreach($assoc as $part => $data)
    {
        // code
    }
}

Or do you need something recursive? I'd be able to help more with example data and a context in how you want the data returned.

或者你需要递归的东西?我将能够在示例数据和您希望如何返回数据的上下文方面提供更多帮助。

回答by fkaufusi

Consider this multi dimentional array, I hope this function will help.

考虑这个多维数组,我希望这个函数会有所帮助。

$n = array('customer' => array('address' => 'Kenmore street',
                'phone' => '121223'),
      'consumer' => 'wellington consumer',
      'employee' => array('name' => array('fname' => 'finau', 'lname' => 'kaufusi'),
                     'age' => 32,
                 'nationality' => 'Tonga')
      );



iterator($n);

function iterator($arr){

    foreach($arr as $key => $val){

    if(is_array($val))iterator($val);

    echo '<p>key: '.$key.' | value: '.$val.'</p>';

    //filter the $key and $val here and do what you want
    }

}