Daha iyi bir fikir "ilk elemanı dizinin dahili göstericisi sarar ve ilk dizi öğesinin değerini verir" reset hangi kullanmak olabilir
Örnek:
function Get1stArrayValue($arr) { return reset($arr); }
As @therefromhere pointed out in the comment below, this solution is not ideal as it changes the state of the internal pointer. However, I don't think it is much of an issue as other functions such as array_pop also reset it.
The main concern that it couldn't be used when iterating over an array isn't an problem as foreach
operates on a copy of the array. The PHP manual states:
Dizi başvurulan sürece, foreach dizi değil dizinin kendisi bir kopyası üzerinde çalışır.
Bu bazı basit test kodu kullanılarak gösterilebilir:
$arr = array("a", "b", "c", "d");
foreach ( $arr as $val ){
echo reset($arr) . " - " . $val . "\n";
}
Sonuç:
a - a
a - b
a - c
a - d