Reverse order of foreach list items

后端 未结 8 1498
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 16:31

I would like to reverse the order of this code\'s list items. Basically it\'s a set of years going from oldest to recent and I am trying to reverse that output.



        
8条回答
  •  抹茶落季
    2020-12-02 16:50

    Assuming you just need to reverse an indexed array (not associative or multidimensional) a simple for loop would suffice:

    $fruits = ['bananas', 'apples', 'pears'];
    for($i = count($fruits)-1; $i >= 0; $i--) {
        echo $fruits[$i] . '
    '; }

提交回复
热议问题