【问题标题】:Echo only last 10 values of array仅回显数组的最后 10 个值
【发布时间】:2015-03-29 14:18:25
【问题描述】:

我已经构建了这个数组

 <?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 行。

我试过数组拼接

<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 为数组,给定为空

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

提前致谢。

【问题讨论】:

    标签: php arrays foreach array-splice


    【解决方案1】:

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

    【讨论】:

    • 你可以用array_slice($bidding_history, -10)简化它。
    【解决方案2】:

    我认为答案可能不在于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'];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      相关资源
      最近更新 更多