php foreach 列表项的倒序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10777597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse order of foreach list items
提问by blkedy
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++;
}
?>
回答by Sampson
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:
如果您正在寻找纯粹的 PHP 解决方案,您也可以简单地通过列表向后计数,从前到后访问它:
$accounts = Array(
'@jonathansampson',
'@f12devtools',
'@ieanswers'
);
$index = count($accounts);
while($index) {
echo sprintf("<li>%s</li>", $accounts[--$index]);
}
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.
以上设置$index为元素总数,然后开始从后到前访问它们,减少下一次迭代的索引值。
Reversing the Array
反转数组
You could also leverage the array_reversefunctionto invert the values of your array, allowing you to access them in reverse order:
您还可以利用该array_reverse函数来反转数组的值,从而以相反的顺序访问它们:
$accounts = Array(
'@jonathansampson',
'@f12devtools',
'@ieanswers'
);
foreach ( array_reverse($accounts) as $account ) {
echo sprintf("<li>%s</li>", $account);
}
回答by Daniel Figueroa
Or you could use the array_reversefunction.
或者您可以使用array_reverse函数。
回答by JasonGabler
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.
array_reverse()不改变源数组,而是返回一个新数组。(请参阅array_reverse()。)所以您要么需要先存储新数组,要么只在 for 循环的声明中使用函数。
<?php
$input = array('a', 'b', 'c');
foreach (array_reverse($input) as $value) {
echo $value."\n";
}
?>
The output will be:
输出将是:
c
b
a
So, to address to OP, the code becomes:
因此,要寻址到 OP,代码变为:
<?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++;
}
Lastly, I'm going to guess that the $jwas either a counter used in an initial attempt to get a reverse walk of $skills_nav, or a way to count the $skills_navarray. 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).
最后,我将猜测$j是用于初始尝试反向遍历 的计数器$skills_nav,或者是对$skills_nav数组进行计数的一种方法。如果是前者,现在应该删除它,因为您有正确的解决方案。如果是后者,则可以在循环外将其替换为$j = count($skills_nav).
回答by Damian Green
If you don't mind destroying the array (or a temp copy of it) you can do:
如果您不介意销毁数组(或它的临时副本),您可以执行以下操作:
$stack = array("orange", "banana", "apple", "raspberry");
while ($fruit = array_pop($stack)){
echo $fruit . "\n<br>";
}
produces:
产生:
raspberry
apple
banana
orange
I think this solution reads cleaner than fiddling with an index and you are less likely to introduce index handling mistakes, but the problem with it is that your code will likely take slightly longer to run if you have to create a temporary copy of the array first. Fiddling with an index is likely to run faster, and it may also come in handy if you actually 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...
但是正如你所看到的,你必须非常小心索引......
回答by Felix Eve
回答by Hugo Trial
If your array is populated through an SQL Query consider reversing the result in MySQL, ie :
如果您的数组是通过 SQL 查询填充的,请考虑在 MySQL 中反转结果,即:
SELECT * FROM model_input order by creation_date desc
回答by user5168728
<?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++;
}
?>

