php - 仅回显数组的最后 10 个值

标签 php arrays foreach array-splice

我已经构建了这个数组

 <?php
    $bidding_history = $current_bidding_data;
    if(is_array($bidding_history) && !empty($bidding_history) ){ 
    ?>

        <ul class="list-group">
        <?php
        foreach($bidding_history as $kk => $bhistory){
        ?>

$bhistory 的回显如下,

<li class="list-group-item"><span class="badge pull-right"><small><?php echo $bhistory['username'] ?></small></span>

我只想回显 $bhistory 的最后 10 行。

我尝试过 array_splice

<li class="list-group-item"><span class="badge pull-right"><small><?php echo array_splice ($bidding_history['username'], -1, 10, true) ?></small></span>

但在前端我收到错误代码: 警告:array_slice() 期望参数 1 为数组,给定 null

我不知道做错了什么,需要帮助

提前致谢。

最佳答案

您可以使用array_slice();来实现此目的。

这里是一个例子:

<?php
$bidding_history_new = array_slice($bidding_history, -10);
foreach($bidding_history_new as $kk => $bhistory){
    //whatever you do here

}
?>

有关 PHP array_slice(); 函数的更多信息:http://php.net/manual/en/function.array-slice.php

关于php - 仅回显数组的最后 10 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29330219/

相关文章:

PHP 方法链还是流利的接口(interface)?

Java - 使用文件名数组对每个文件运行方法

php - 显示 channel 中的视频列表 - Vimeo 高级 API

php - 使用 PHP 从 MySQL 获取信息时遇到问题

PHP fopen($url,w)

javascript - 如果一个数组的所有项目存在于另一个数组中,则将其删除

javascript - 如何使用 native JavaScript 合并数组内的对象

c# - 如何在 C# 中的自定义对象上使用 foreach 关键字

php - 在 foreach 中,isLastItem() 存在吗?

c++ - for_each 调用不适用于指针 vector