多维数组Foreach - 在不同的表中显示
我目前有以下数组:(
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种解决方法。获取第一个数组项并将其用作标题。然后使用第一项中的键循环遍历数组的其余部分以显示结果。显然,我的显示代码既快又脏,但这应该可以帮助您入门。
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.