多维数组Foreach - 在不同的表中显示

发布于 2024-12-07 00:53:35 字数 1485 浏览 0 评论 0原文

我目前有以下数组:(

Array
(
    [group] => Array
        {
            [5] => group1-title
            [6] => group2-title
        }
    [treatment] => Array
        (
            [5] => Array
                (
                    [16] => Array
                        (
                            [title] => title1
                            [description] => description1
                            [price] => price1
                        )

                    [17] => Array
                        (
                            [title] => title2
                            [description] => description2
                            [price] => price2
                        )
            [6] => Array
                (

                    [21] => Array
                        (
                            [title] => title3
                            [description] => description3
                            [price] => price3
                        )
                }
        }
}

实际上,数组数据很大,因此已被裁剪以给出上面的示例)

我尝试做的是在数组上运行 foreach 循环,以便能够将数据分组为定价表按其组分开(数组键 [5][6]

我想要的最终结果如下:

Group1-Title


标题1 | 描述 |价格
标题 2 |描述 |价格


Group2-Title


标题 3 |描述 |到目前为止,


我已经尝试使用带有内部 foreach 循环的 foreach 循环。这可以很好地提取细节和回声 - 但是我无法弄清楚如何在 foreach 循环期间成功对数据进行分组。

I currently have the following array:

Array
(
    [group] => Array
        {
            [5] => group1-title
            [6] => group2-title
        }
    [treatment] => Array
        (
            [5] => Array
                (
                    [16] => Array
                        (
                            [title] => title1
                            [description] => description1
                            [price] => price1
                        )

                    [17] => Array
                        (
                            [title] => title2
                            [description] => description2
                            [price] => price2
                        )
            [6] => Array
                (

                    [21] => Array
                        (
                            [title] => title3
                            [description] => description3
                            [price] => price3
                        )
                }
        }
}

(In reality the array data is large so it has been cropped to give an example above)

What I am attempting to do is run a foreach loop over the array to enable to me group the data in a pricing table separate by their groups (the array keys [5] and [6])

The end result I am aiming for is the following:

Group1-Title


Title 1 | Desc | Price
Title 2 | Desc | Price


Group2-Title


Title 3 | Desc | Price


I have attempted so far with a foreach loop with an inner foreach loop. That pulls the details and echos out fine - however I can't figure out how to successfully group the data during the foreach loop.

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

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

发布评论

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

评论(2

哭泣的笑容 2024-12-14 00:53:35

这是一种解决方法。获取第一个数组项并将其用作标题。然后使用第一项中的键循环遍历数组的其余部分以显示结果。显然,我的显示代码既快又脏,但这应该可以帮助您入门。

foreach($your_array['group'] as $iGroupId => $sTitle) {
    echo 'Group: ' . $sTitle . "\n";
    foreach($your_array['treatment'][$iGroupId] as $aGroup) {
        echo implode('|', $aGroup); . "\n"
    }
}

Here's one way to go about it. Grab the first array item and use that for the titles. Then loop through the rest of your array using the keys from the first item to display the results. Obviously my display code is quick and dirty, but this should get you started.

foreach($your_array['group'] as $iGroupId => $sTitle) {
    echo 'Group: ' . $sTitle . "\n";
    foreach($your_array['treatment'][$iGroupId] as $aGroup) {
        echo implode('|', $aGroup); . "\n"
    }
}
自此以后,行同陌路 2024-12-14 00:53:35
$data = array(); //your data array above

//loop through the groups    
foreach($data['group'] as $key=>$group){
    //echo group header and table start
    echo "<h1>$group</h1>";
    echo "<table border=1>";
    //loop through the data with the corresponding group key
    foreach($data['treatment'][$key] as $row){
        //echo row data
        echo "<tr>";
        echo "<td>{$row['title']}</td>";
        echo "<td>{$row['description']}</td>";
        echo "<td>{$row['price']}</td>";
        echo "</tr>";
    }
    //close table.
    echo "</table>";
}
$data = array(); //your data array above

//loop through the groups    
foreach($data['group'] as $key=>$group){
    //echo group header and table start
    echo "<h1>$group</h1>";
    echo "<table border=1>";
    //loop through the data with the corresponding group key
    foreach($data['treatment'][$key] as $row){
        //echo row data
        echo "<tr>";
        echo "<td>{$row['title']}</td>";
        echo "<td>{$row['description']}</td>";
        echo "<td>{$row['price']}</td>";
        echo "</tr>";
    }
    //close table.
    echo "</table>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文