[php]key — 從關聯陣列中取得鍵名

範例

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo "The key from the current position is: " . key($people);
?>

輸出:

The key from the current position is: 0

官方範例
<?php
$array = array(
 'fruit1' => 'apple',
 'fruit2' => 'orange',
 'fruit3' => 'grape',
 'fruit4' => 'apple',
 'fruit5' => 'apple');

// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array).'<br />';
    }
    next($array);
}
?>

輸出

fruit1
fruit4
fruit5


arrow
arrow
    全站熱搜

    stockwfj3 發表在 痞客邦 留言(0) 人氣()