php array 處理函數

來源:互聯網
上載者:User
current

current -- 返回數組中的當前單元

說明

mixed current ( array &array )

每個數組中都有一個內部的指標指向它“當前的”單元,初始指向插入到數組中的第一個單元。

current() 函數返回當前被內部指標指向的數組單元的值,並不移動指標。如果內部指標指向超出了單元列表的末端,current() 返回 FALSE。

警告

如果數組包含有空的單元(0 或者 "",Null 字元串)則本函數在碰到這個單元時也返回FALSE。這使得用 current() 不可能判斷是否到了此數組列表的末端。要正確遍曆可能含有空單元的數組,用 each() 函數。

例 1. current() 及相關函數的用法樣本

$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport);
// $mode = 'foot';
$mode = next($transport);
// $mode = 'bike';
$mode = current($transport);
// $mode = 'bike';
$mode = prev($transport);
// $mode = 'foot';
$mode = end($transport);
// $mode = 'plane';
$mode = current($transport);
// $mode = 'plane';
?>

-------------------------------------------------------------------------------------------------------

end

end -- 將數組的內部指標指向最後一個單元

說明

mixed end ( array &array )

end() 將 array 的內部指標移動到最後一個單元並返回其值。

例 1. 簡單的 end() 例子

$fruits = array('apple','banana','cranberry');
echo end($fruits);
// cranberry
?>

-------------------------------------------------------------------------------------------------------

prev

prev -- 將數組的內部指標倒回一位

說明

mixed prev ( array &array )

返回數組內部指標指向的前一個單元的值,或當沒有更多單元時返回 FALSE。

警告

如果數組包含空的單元,或者單元的值是 0 則本函數碰到這些單元也返回 FALSE。要正確遍曆可能含有空單元或者單元值為 0 的數組,參見 each() 函數。

prev() 和 next() 的行為類似,只除了它將內部指標倒回一位而不是前移一位。

例 1. prev() 及相關函數用法樣本

$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport);
// $mode = 'foot';
$mode = next($transport);
// $mode = 'bike';
$mode = next($transport);
// $mode = 'car';
$mode = prev($transport);
// $mode = 'bike';
$mode = end($transport);
// $mode = 'plane';
?>

-------------------------------------------------------------------------------------------------------

next

next -- 將數組中的內部指標向前移動一位

說明

mixed next ( array &array )

返回數組內部指標指向的下一個單元的值,或當沒有更多單元時返回 FALSE。

next() 和 current() 的行為類似,只有一點區別,在傳回值之前將內部指標向前移動一位。這意味著它返回的是下一個數組單元的值並將數組指標向前移動了一位。如果移動指標的結果是超出了數組單元的末端,則 next() 返回 FALSE。

警告

如果數組包含空的單元,或者單元的值是 0 則本函數碰到這些單元也返回 FALSE。要正確遍曆可能含有空單元或者單元值為 0 的數組,參見 each() 函數。

例 1. next() 及相關函數的用法樣本

$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport);
// $mode = 'foot';
$mode = next($transport);
// $mode = 'bike';
$mode = next($transport);
// $mode = 'car';
$mode = prev($transport);
// $mode = 'bike';
$mode = end($transport);
// $mode = 'plane';
?>

-------------------------------------------------------------------------------------------------------

key

key -- 從 關聯陣列中取得鍵名

說明

mixed key ( array &array )

key() 返回數組中當前單元的鍵名。

例 1. key() 例子

$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).'
'
;
}
next($array
);
}
?>

-------------------------------------------------------------------------------------------------------

reset

reset -- 將數組的內部指標指向第一個單元

說明

mixed reset ( array &array )

reset() 將 array 的內部指標倒回到第一個單元並返回第一個數組單元的值,如果數組為空白則返回 FALSE。

例 1. reset() 例子


$array = array('step one', 'step two', 'step three', 'step four'
);

// by default, the pointer is on the first element
echo current($array) . "
/n";
// "step one"

// skip two steps
next($array
);
next($array
);
echo current($array) . "
/n";
// "step three"

// reset pointer, start again on step one
reset($array
);
echo current($array) . "
/n";
// "step one"

?>

-------------------------------------------------------------------------------------------------------

each

each -- 返回數組中當前的鍵/值對並將數組指標向前移動一步

說明

array each ( array &array )

返回 array 數組中當前指標位置的鍵/值對並向前移動數組指標。索引值對被返回為四個單元的數組,鍵名為 0,1,key 和 value。單元 0 和 key 包含有數組單元的鍵名,1 和 value 包含有資料。

如果內部指標越過了數組的末端,則 each() 返回 FALSE。

例 1. each() 例子

$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo
);
print_r($bar
);
?>

$bar 現在包含有如下的鍵/值對:

Array { [1] => bob [value] => bob [0] => 0 [key] => 0 }

$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each($foo
);
print_r($bar
);
?>

$bar 現在包含有如下的鍵/值對:

Array { [1] => Bob [value] => Bob [0] => Robert [key] => Robert }

each() 經常和 list() 結合使用來遍曆數組,例如:

例 2. 用 each() 遍曆數組

$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit
);
while (list($key, $val) = each($fruit
)) {
echo "$key => $val/n"
;
}
?>

上例將輸出:

a => apple b => banana c => cranberry

在執行 each() 之後,數組指標將停留在數組中的下一個單元或者當碰到數組結尾時停留在最後一個單元。如果要再用 each 遍曆數組,必須使用 reset()。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.