Php 如何将foreach循环设置为从最后一个数组开始?

Php 如何将foreach循环设置为从最后一个数组开始?,php,foreach,Php,Foreach,如何设置foreach循环以开始查看数组的最后一个条目,然后每个循环将向后而不是向前 谢谢。您可以只使用阵列: $reverse = array_reverse($array, true); // true to preserve keys foreach($reverse as $key => $value) { /* etc. */ } 或者,如果您确定数组只包含数字键,这可能会更快: for($i = count($array) - 1; $i >= 0; $i--) {

如何设置foreach循环以开始查看数组的最后一个条目,然后每个循环将向后而不是向前

谢谢。

您可以只使用阵列:

$reverse = array_reverse($array, true); // true to preserve keys
foreach($reverse as $key => $value) { /* etc. */ }
或者,如果您确定数组只包含数字键,这可能会更快:

for($i = count($array) - 1; $i >= 0; $i--) {
  /* etc. */
}
array\u reverse函数将反转数组。

您可以执行以下操作:

$values = array();
$max = count($values);

foreach($i = $max; $i > 0; $i--) {
    $key = $values[$i];
    // do something with the key
}
这是一个复制品
$values = array();
$max = count($values);

foreach($i = $max; $i > 0; $i--) {
    $key = $values[$i];
    // do something with the key
}