Reverse ordering of foreach list items

I would like to reorder these code list items. In fact, this is a set of years from the old until recently, and I'm trying to change this conclusion.

<?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++; } ?> 
+48
php
May 27 '12 at 10:01
source share
8 answers

Walk back

If you are looking for a purely PHP solution, you can also just count back through the list, access it back and forth:

 $accounts = Array( '@jonathansampson', '@f12devtools', '@ieanswers' ); $index = count($accounts); while($index) { echo sprintf("<li>%s</li>", $accounts[--$index]); } 

The above $index values ​​refer to the total number of elements, and then begin accessing them backward, decreasing the index value for the next iteration.

Array reversal

You can also use the array_reverse function to invert the values ​​of your array, allowing you to access them in the reverse order:

 $accounts = Array( '@jonathansampson', '@f12devtools', '@ieanswers' ); foreach ( array_reverse($accounts) as $account ) { echo sprintf("<li>%s</li>", $account); } 
+98
May 27 '12 at 22:04
source share

Or you can use the array_reverse function.

+24
May 27 '12 at 22:05
source share

array_reverse() does not change the original array, but returns a new array. (See array_reverse() .) You need to either save the new array first, or simply use the function in the for loop declaration.

 <?php $input = array('a', 'b', 'c'); foreach (array_reverse($input) as $value) { echo $value."\n"; } ?> 

The output will be:

 c b a 

So, to refer to the OP, the code would look like this:

 <?php $j=1; foreach ( array_reverse($skills_nav) as $skill ) { $a = '<li><a href="#" data-filter=".'.$skill->slug.'">'; $a .= $skill->name; $a .= '</a></li>'; echo $a; echo "\n"; $j++; } 

Finally, I'm going to assume that $j was either the counter used in the initial attempt to get the $skills_nav , or the way to count the $skills_nav . If the first one, it should be removed now that you have the right solution. If the latter, it can be replaced outside the loop with $j = count($skills_nav) .

+6
Oct 31 '15 at 18:25
source share

You can use usort to create your own sorting rules.

+3
May 27 '12 at 22:02
source share

Assuming you just need to undo an indexed array (not associative or multidimensional), simple enough for a loop :

 $fruits = ['bananas', 'apples', 'pears']; for($i = count($fruits)-1; $i >= 0; $i--) { echo $fruits[$i] . '<br>'; } 
+2
Nov 30 '16 at 4:47
source share

If you don't mind destroying the array (or a temporary copy of it), you can do:

 $stack = array("orange", "banana", "apple", "raspberry"); while ($fruit = array_pop($stack)){ echo $fruit . "\n<br>"; } 

gives:

 raspberry apple banana orange 

I think this solution is easier to read than messing around with the index, and you are less likely to introduce index processing errors, but the problem is that your code will most likely take a little longer if you have to create a temporary copy of the array first . Index twisting is likely to work faster, and it can also come in handy if you really need to reference the index, as in:

 $stack = array("orange", "banana", "apple", "raspberry"); $index = count($stack) - 1; while($index > -1){ echo $stack[$index] ." is in position ". $index . "\n<br>"; $index--; } 

But, as you can see, you have to be very careful with the index ...

+2
Jun. 19 '17 at 2:20
source share

If your array is populated with SQL Query, consider changing the result in MySQL, namely:

 SELECT * FROM model_input order by creation_date desc 
+1
Jan 26 '16 at 2:02 on
source share
 <?php $j=1; array_reverse($skills_nav); foreach ( $skills_nav as $skill ) { $a = '<li><a href="#" data-filter=".'.$skill->slug.'">'; $a .= $skill->name; $a .= '</a></li>'; echo $a; echo "\n"; $j++; } ?> 
-one
Jul 29 '15 at 9:33
source share



All Articles