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

后端 未结 11 1998
清酒与你
清酒与你 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条回答
  •  旧时难觅i
    2020-12-05 14:32

    $array=array(
        0 => 0,
        '1' => 1,
        2 => null,
        3 => false
    );
    
    $value=end( $array );                      // ← value for first iteration
    while(($key=key( $array )) !== null) {
      echo "key=$key, value=$value\n";
    
      $value=prev( $array );                   // ← value for next iteration
    }
    

提交回复
热议问题