doutan2456 2012-08-01 14:11
浏览 21

使用属性从多维数组中删除项目

I am trying to display an array of data produced from an sql query. this works fine. However I now wish not to display 1 item where the id attribute = a value. code:

<?php foreach($sheet_list as $key => $sheet) : ?> 
        <?php if($sheet['id'] == $selected1){unset ($sheet_list[$key]);}?>
        <tr>
            <td><?=$sheet['title'];?></td>
            <td><?php echo date('d F Y', strtotime($sheet['startDate'])); ?></td>
            <td><?php echo date('d F Y', strtotime($sheet['endDate'])); ?></td>
            <td><input type="radio" name="selected2" id="selected2" value="<?=$sheet['id'];?>" <?php 
            if ($key == 0) echo ' checked ';
            ?>/></td>
        </tr>
        <?php endforeach; ?>

so this is producing a table which is fine, however it is in the foreach loop where i want NOT to display 1 array item and all its attributes where the "id" field is = to the variable $selected1 I have seen lots of questions posted here, however many for 1 or two dimensional arrays. Please help.

  • 写回答

2条回答 默认 最新

  • dongyihao9887 2012-08-01 14:15
    关注

    You're close, you should be using continue; instead of unset():

    <?php if($sheet['id'] == $selected1){ continue; } ?>
    

    continuePHP Docs makes the loop skip to the next iteration, effectively skipping the current iteration, which is what you want to do.

    The benefit here is that you never modify for you $sheet_list array, unlike with unset() where you'll remove that item from the array.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大