限制多维数组响应

发布于 2024-12-29 22:40:57 字数 1191 浏览 0 评论 0原文

有 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 fors 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 技术交流群。

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

发布评论

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

评论(2

森罗 2025-01-05 22:40:57

启动 foreach 后,添加:

if($i > 19) {
    break;
}

这会检查 $i 是否大于 19(这意味着 20 次迭代),然后中断此 foreach 循环。有关中断的更多信息,此处

After you start the foreach, add:

if($i > 19) {
    break;
}

This checks if $i is greater than 19 (which means 20 iterations) and then breaks this foreach loop. More information about break, here.

青春有你 2025-01-05 22:40:57

您可以这样做:

$i = 0;
foreach ($value->channel->item as $item)
{
     if($i > 19) {
       break;
     }
     $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++;
}

这会给您 20 个项目。

希望这是您想要的:)

You can do it like :

$i = 0;
foreach ($value->channel->item as $item)
{
     if($i > 19) {
       break;
     }
     $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++;
}

This will give you 20 items.

Hope this is what you want :)

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