限制多维数组响应
有 3 个多维数组,我对其执行 foreach
如何将 foreach 内的多维响应从 X 项限制为 20 。
代码:
$i = 0;
foreach ($value->channel->item as $item)
{
$data['data'][$keySection]['item1'][$i]['url'] = $item->url;
$data['data'][$keySection]['item1'][$i]['title'] = $item->title;
$data['data'][$keySection]['item1'][$i]['img'] = $item->thumb;
$i++;
}
其中 $value
包含在
foreach ($homeData as $keySection => $valueSection)
{
foreach($valueSection as $key => $value)
{
switch ($key)
{
我尝试在 foreach 中应用一些
与外部一样,但我就是无法让它正常工作,我要么得到双倍的结果,要么根本不工作。for
($value->channel->item as $item )
我怎样才能做到这一点?
编辑: $i
与它无关...我需要限制 $value->channel->item
其中 item
包含 X结果
编辑2: $i
用于 $homeData
,其中 $homeData
包含三个值,其中每个值稍后将包含 3 个不同的 $ 值value->channel->item
因此,如果 item 包含 20 个结果,则为 3x20 = 60,并且 $i 用于分隔每 20 个结果...
Edit3: 好的,现在我明白了...抱歉造成误解
Having 3 multidimensional arrays, to whom I do a foreach
how can I limit the multidimensional response inside foreach from X items to lets say 20.
Code:
$i = 0;
foreach ($value->channel->item as $item)
{
$data['data'][$keySection]['item1'][$i]['url'] = $item->url;
$data['data'][$keySection]['item1'][$i]['title'] = $item->title;
$data['data'][$keySection]['item1'][$i]['img'] = $item->thumb;
$i++;
}
where $value
is contained within
foreach ($homeData as $keySection => $valueSection)
{
foreach($valueSection as $key => $value)
{
switch ($key)
{
I've tried aplying some for
s both within foreach ($value->channel->item as $item)
as outside but I just can't get it to work properly, I get either doubled results or not working at all.
How can I make this work??
Edit:$i
has nothing to do with it... I need to limit $value->channel->item
where item
contains X results
Edit2:$i
is for $homeData
where $homeData
contains three values and each and one of those will later contain 3 different values of $value->channel->item
so if item contains 20 results, will be 3x20 = 60 and $i is ment to separate each 20 results...
Edit3:
ok, now I get it... sorry for the misunderstanding
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
启动 foreach 后,添加:
这会检查 $i 是否大于 19(这意味着 20 次迭代),然后中断此 foreach 循环。有关中断的更多信息,此处。
After you start the foreach, add:
This checks if $i is greater than 19 (which means 20 iterations) and then breaks this foreach loop. More information about break, here.
您可以这样做:
这会给您 20 个项目。
希望这是您想要的:)
You can do it like :
This will give you 20 items.
Hope this is what you want :)