【问题标题】:Loop within loop used on multi-dimensional array to group data possible?多维数组上使用的循环内循环可以对数据进行分组吗?
【发布时间】:2013-04-04 14:53:25
【问题描述】:

我正在尝试从多维数组中输出分组数据。考虑最高级别的数组键“组”,然后在组内是单独的搜索行。

例如$group[ 0 ][ 1 ] 将是“第 1 组,第 1 行”。

我想显示每个组中的所有行,然后通过插入“<hr />”标签来表示该组已更改。目前第一组正确显示,然后显示 hr 标签,但没有显示第二组结果。我的循环内循环方法错误吗?是否可以使用这样的多维数组? 谢谢!!

我的数组如下所示:

Array
(
[0] => Array
    (
        [0] => stdClass Object
            (
                [name] => testing
                [searchID] => 131
                [lineID] => 190
                [searchString] => 1
            )

        [1] => stdClass Object
            (
                [name] => testing
                [searchID] => 131
                [lineID] => 191
                [searchString] => 2
            )

        [2] => stdClass Object
            (
                [name] => testing
                [searchID] => 131
                [lineID] => 192
                [searchString] => 3
            )

        [3] => stdClass Object
            (
                [name] => testing
                [searchID] => 131
                [lineID] => 193
                [searchString] => 4
            )

    )

[1] => Array
    (
        [4] => stdClass Object
            (
                [name] => test2
                [searchID] => 132
                [lineID] => 199
                [searchString] => 1
            )

        [5] => stdClass Object
            (
                [name] => test2
                [searchID] => 132
                [lineID] => 200
                [searchString] => 2
            )

    )

)

我的代码如下所示:

 $x = 0;
 $y = 0;
 while( $x < count( $groups ) )
 {
 while( $y < count( $groups[ $x ] ) )
 {
     //display each single search string
    echo $groups[ $x ][ $y ]->searchString.'<br>';
    $y++; 
 }
 echo '<hr>';
 $x++;
 }

【问题讨论】:

    标签: php arrays loops multidimensional-array


    【解决方案1】:

    你可以试试

    foreach ( $groups as $group ) {
        foreach ( $group as $var ) {
            echo $var->searchString, "<br />";
        }
    }
    

    【讨论】:

    • 第二个循环不应该在组上吗?不是组
    【解决方案2】:

    查看您的示例数组,第二组的 y 值应从 4 开始并循环直到达到 6。

    我建议您使用 foreach 循环。 http://php.net/manual/en/control-structures.foreach.php

    您的代码如下所示:

    foreach ( $groups as $gKey => $gValue )
    {
        foreach ( $gValue as $key => $value )
        {
            echo $groups[$gKey][$key]->searchString . "<br />";
            // or $value->searchString . "<br />";
        }
        echo "<hr />";
    }
    

    【讨论】:

    • 这行得通,而且比我的方式简单优雅得多。我以后肯定会这样做,谢谢! :)
    【解决方案3】:

    您需要在第一个循环内重置y

    $x = 0;
    while( $x < count( $groups ) )
    {
        $y = 0;
        while( $y < count( $groups[ $x ] ) )
        {
            //display each single search string
            echo $groups[ $x ][ $y ]->searchString.'<br>';
            $y++; 
        }
         echo '<hr>';
         $x++;
    }
    

    【讨论】:

    • 我不这么认为,因为每个新组的第二个键( [x] >> [y]
    • 注意:未定义的偏移量:0 in ... on line 423
    • @KelseyThorpe 然后使用 while 将不起作用,因为第二个循环上的 count($groups[$x]$y]) 将为 2,但 y 将等于 4
    【解决方案4】:

    试试 foreach:

    foreach ($groups as $x => $group )
    {
        foreach ($group as $y => $subGroup )
        {
            echo $subGroup->searchString . '<br>';
        }
        echo '<hr>';
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多