简体   繁体   English

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

[英]Iterating over a complex Associative Array in PHP

Is there an easy way to iterate over an associative array of this structure in PHP: 有没有一种简单的方法可以在PHP中迭代这个结构的关联数组:

The array $searches has a numbered index, with between 4 and 5 associative parts. 数组$searches具有编号索引,具有4到5个关联部分。 So I not only need to iterate over $searches[0] through $searches[n] , but also $searches[0]["part0"] through $searches[n]["partn"] . 所以,我不仅需要遍历$searches[0]通过$searches[n]$searches[0]["part0"]通过$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? 想要以一种美好,整洁,易懂的方式做到这一点?

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 看到

Nest two foreach loops : 嵌套两个foreach循环

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

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
    }
}

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";
    }
}

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
    }

}

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

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. 我将能够通过示例数据和您希望数据返回的上下文来提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM