Iterate in reverse through an array with PHP - SPL solution?

后端 未结 11 1961
清酒与你
清酒与你 2020-12-05 13:51

Is there an SPL Reverse array iterator in PHP? And if not, what would be the best way to achieve it?

I could simply do

$array = array_reverse($array)         


        
11条回答
  •  伪装坚强ぢ
    2020-12-05 14:26

    Based on linepogl's answer... You can make it even more efficient by avoiding current() call

    for ($value = end($array); ($key = key($array)) !== null; $value = prev($array)) {
         // ... do something with $key => $value
    }
    

提交回复
热议问题