doucaigai7176 2015-02-24 16:16
浏览 14

我如何对数组进行升序而不是随机排序

I found this awesome snippet online. It shows a new testimonial at random on refresh of the page and am wondering how to show the array in an ascending order rather than random?

$target = sort(0, $num-1); 

^ This was my attempt

<?php
    /*
    --------------------------------------------
    Random Testimonial Generator Created by:
    Ryan McCormick
    Ntech Communications
    Website: http://www.ntechcomm.com/
    Blog: http://www.ntechcomm.com/blog/
    Twitter: @ntechcomm
    --------------------------------------------
    */

    //Start Array
    $testimonials = array();
    $testimonials[0] = "Testimonial 1";
    $testimonials[1] = "Testimonial 2";
    $testimonials[2] = "Testimonial 3";
    $testimonials[3] = "Testimonial 4";
    //Automate script by counting all testimonials
    $num = count($testimonials);
    //randomize target testimonial
    $target = rand(0, $num-1);
    /*
    To display testimonials on site
    --------------------------------------------
    place the following code in the
    display area:
    <?php echo $testimonials[$target]; ?>
    --------------------------------------------
    Use a PHP include to use this code on your
    target page.
    */
    ?>

Output the testimonial in page with:

<?php echo $testimonials[$target]; ?>

To clarify:

The code i posted displays one testimonial randomly on refresh of the page. I would like it to keep this function and display only one at a time but I want them to display in the order they are added.

  • 写回答

2条回答 默认 最新

  • dongpao1921 2015-02-24 16:22
    关注

    Sort using ascending order

    $testimonials = array();
    $testimonials[0] = "Testimonial 1";
    $testimonials[1] = "Testimonial 2";
    $testimonials[2] = "Testimonial 3";
    $testimonials[3] = "Testimonial 4";
    
    $random  = rand(0, count($testimonials) - 1);
    $asc_arr = sort($testimonials);
    print_r($result);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?