duanlou2917 2012-01-30 12:20
浏览 34
已采纳

限制多维数组响应

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

  • 写回答

2条回答 默认 最新

  • duanchu9914 2012-01-30 12:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题