仅回显数组的最后10个值

时间:2015-03-29 14:18:26

标签: 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

我不知道出了什么问题,需要帮助

提前致谢。

2 个答案:

答案 0 :(得分:2)

您可以使用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

答案 1 :(得分:0)

我认为答案可能并不在于array_slice

使用for循环可以很容易地查看数组的最后10个元素:

for($i = count($bidding_history) - 10; $i < count($bidding_history); $i++) {
?>
    <li class="list-group-item"><span class="badge pull-right"><small>
<?php 
    echo $bidding_history[$i]['username'] 
?>
    </small></span>
<?php
}

或者

for($i = count($bidding_history) - 10; $i < count($bidding_history); $i++) {
    //...whatever you want to do...
    $username = $bidding_history[$i]['username'];
}