PHP pos()関数
インスタンス
配列内の現在の要素の値を出力:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo pos($people) . "<br>";
?>
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo pos($people) . "<br>";
?>
インスタンスを実行 »
定義と使い方
pos() 関数は配列内の現在の要素の値を返します。
この関数はcurrent()関数のエイリアスです。
各配列には内部ポインタがあり、その「現在の」要素を指しています。初期状態では、配列に挿入された最初の要素を指しています。
ヒント:この関数は配列の内部ポインタを移動しません。
関連するメソッド:
- current()- 配列内の現在の要素の値を返します。
- end()- 内部ポインタを配列内の最後の要素に移動し、出力します。
- next()- 内部ポインタを配列内の次の要素に移動し、出力します。
- prev()- 内部ポインタを配列内の前の要素に移動し、出力します。
- reset()- 内部ポインタを配列内の最初の要素に移動し、出力します。
- each()- 現在の要素のキー名とキー値を返し、内部ポインタを前方に移動します。
構文
pos(array)
| パラメータ | 説明 |
|---|---|
| array | 必須。使用する配列を指定します。 |
技術詳細
| 戻り値: | 配列内の現在の要素の値を返します。現在の要素が空であるか、現在の要素に値がない場合は FALSE を返します。 |
|---|---|
| PHP バージョン: | 4+ |
その他のインスタンス
インスタンス 1
すべての関連メソッドのデモ:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>"; // The current element is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
echo current($people) . "<br>"; // Now the current element is Joe
echo prev($people) . "<br>"; // The previous element of Joe is Peter
echo end($people) . "<br>"; // The last element is Cleveland
echo prev($people) . "<br>"; // The previous element of Cleveland is Glenn
echo current($people) . "<br>"; // Now the current element is Glenn
echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
print_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward
?>
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>"; // The current element is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
echo current($people) . "<br>"; // Now the current element is Joe
echo prev($people) . "<br>"; // The previous element of Joe is Peter
echo end($people) . "<br>"; // The last element is Cleveland
echo prev($people) . "<br>"; // The previous element of Cleveland is Glenn
echo current($people) . "<br>"; // Now the current element is Glenn
echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
print_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward
?>
インスタンスを実行 »
完全な PHP Array リファレンスマニュアル その他の拡張