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.
<?php
$j=1;
foreach ( $skills_nav as $skill ) {
$a = '<li><a href="#" data-filter=".'.$skill->slug.'">';
$a .= $skill->name;
$a .= '</a></li>';
echo $a;
echo "\n";
$j++;
}
?>
Walking Backwards
If you’re looking for a purely PHP solution, you can also simply count backwards through the list, access it front-to-back:
The above sets
$indexto the total number of elements, and then begins accessing them back-to-front, reducing the index value for the next iteration.Reversing the Array
You could also leverage the
array_reversefunction to invert the values of your array, allowing you to access them in reverse order: