Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Foreach循环仅获取最后2个数组_Php_Mysql_Arrays_Foreach - Fatal编程技术网

Php 使用Foreach循环仅获取最后2个数组

Php 使用Foreach循环仅获取最后2个数组,php,mysql,arrays,foreach,Php,Mysql,Arrays,Foreach,可能重复: 我只需要它的最后两张唱片。 下面的代码返回所有记录。多谢各位 <?php foreach ($fields as $field) { $type = $field['s_type']; $label = $field['s_label']; $value = Attributes::newInstance()->getValue($item_id, $field['pk_i_id']); if (

可能重复:

我只需要它的最后两张唱片。 下面的代码返回所有记录。多谢各位

<?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 } ?>


您可以使用array\u slice并将-2作为第二个参数吗?

您可以在迭代之前使用array slice来管理字段

$last_two = array_slice($fields, -2);

foreach($last_two as $field) { ... }

虽然如果这是脚本中唯一要做的事情,我建议重写您的查询结构。

很抱歉,$fileds来自下面的函数$fields=Attributes::newInstance()->getFields();感谢Hakre的帮助,这就是我正在寻找的foreach(数组_切片($fields,-2)作为$field){@user1956624不要使用它,除非你想在每次迭代中计算切片数组……我在右边的相关问题列表中找到了重复项。只需搜索一点,你就会更快地找到答案;)-并且在奥斯汀的回答中也看到了提示,关于你应该以只返回最后两个的方式查询数据库。t对于数据库查询,应该这样做。