dongpangfu6322
dongpangfu6322
2013-01-08 01:06
浏览 102
已采纳

使用Foreach循环获取最后2个数组[重复]

Possible Duplicate:
PHP foreach() return only last 50 items

I need only the last 2 record from it. The code below return all the records. thank you

<?php 
    foreach ($fields as $field) {
        $type = $field['s_type'];
        $label = $field['s_label'];
        $value = Attributes::newInstance()->getValue($item_id, $field['pk_i_id']);
        if ($type == 'checkbox') {
            if ($value == 'checked') $value = 'Yes';
            else $value = 'No';
        }
?>
        <tr>
            <td style='width: 150px;'><?php _e($label, pg); ?></td>
            <td style='width: 150px;'><?php _e($value, pg); ?></td>
        </tr>

<?php } ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqybjj3497
    dqybjj3497 2013-01-08 01:36
    已采纳

    Can you use array_slice with -2 as the second parameter?

    点赞 评论
  • dtcrw26206
    dtcrw26206 2013-01-08 01:36

    You can use array slice to manage the fields before iterating.

    $last_two = array_slice($fields, -2);
    
    foreach($last_two as $field) { ... }
    

    Although if this is the only thing being done in this script I would suggest rewriting your query structure.

    点赞 评论

相关推荐