Reverse order of foreach list items

后端 未结 8 1512
佛祖请我去吃肉
佛祖请我去吃肉 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:51

    array_reverse() does not alter the source array, but returns a new array. (See array_reverse().) So you either need to store the new array first or just use function within the declaration of your for loop.

    
    

    The output will be:

    c
    b
    a
    

    So, to address to OP, the code becomes:

    ';
            $a .= $skill->name;                 
            $a .= '
  • '; echo $a; echo "\n"; $j++; }

    Lastly, I'm going to guess that the $j was either a counter used in an initial attempt to get a reverse walk of $skills_nav, or a way to count the $skills_nav array. If the former, it should be removed now that you have the correct solution. If the latter, it can be replaced, outside of the loop, with a $j = count($skills_nav).

提交回复
热议问题