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

发布于 2024-07-04 12:29:55 字数 326 浏览 9 评论 0原文

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

数组 $searches 有一个编号索引,包含 4 到 5 个关联部分。 因此,我不仅需要通过 $searches[n] 迭代 $searches[0],还需要 $searches[0]["part0"]< /code> 到 $searches[n]["partn"]。 困难的部分是不同的索引有不同数量的部分(有些可能会缺少一两个)。

是否想过以一种友好、简洁且易于理解的方式来做这件事?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧人九事 2024-07-11 12:29:55

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

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

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

参见

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

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

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

See

不念旧人 2024-07-11 12:29:55

嵌套两个 foreach 循环

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

Nest two foreach loops:

foreach ($array as $i => $values) {
    print "$i {\n";
    foreach ($values as $key => $value) {
        print "    $key => $value\n";
    }
    print "}\n";
}
安穩 2024-07-11 12:29:55

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

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

}

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
    }

}
套路撩心 2024-07-11 12:29:55

我真的不确定你在这里的意思 - 一对 foreach 循环肯定可以满足你的需要吗?

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

或者你需要一些递归的东西? 我可以通过示例数据和您希望如何返回数据的上下文提供更多帮助。

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

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.

帅的被狗咬 2024-07-11 12:29:55

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

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

骑趴 2024-07-11 12:29:55

您应该能够使用

php 手册 中的嵌套 foreach 语句

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

You should be able to use a nested foreach statment

from the php manual

/* 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";
    }
}
走过海棠暮 2024-07-11 12:29:55

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

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

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文