如果你有很大的一個(gè)數(shù)組,而所要完成的僅是找出一個(gè)存在的給定值,你 " /> 69久成人做爰电影,97精品国产福利一区二区三区,亚洲视频分类

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

PHP 數(shù)組實(shí)例說(shuō)明

php4.0中共有超過(guò)30個(gè)新的數(shù)組相關(guān)函數(shù)。其中很多通用函數(shù)允許你檢查給定數(shù)組中是否存在特定對(duì)象、對(duì)數(shù)組元素計(jì)數(shù)、增加或刪除元素,或?qū)υ嘏判颉?

如果你有很大的一個(gè)數(shù)組,而所要完成的僅是找出一個(gè)存在的給定值,你可以使用in_array()以返回true 或 false。如下代碼將輸出“Not found in this array”――因?yàn)槟銓⒃?namesArray中尋找一個(gè)并不存在的“Alber ”。


<? $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");

$lookingFor = "Albert";

if (in_array($lookingFor, $namesArray)) {

echo "You've found it!";

} else {

echo "Not found in this array!";

}

?>
如果你改變了$lookingFor的值,將其變?yōu)椤癕ary”,你將得到消息“You've found it!”――因?yàn)椤癕ary”是$namesArray的一部分。

如果希望對(duì)數(shù)組元素計(jì)數(shù),你可以使用count()函數(shù):


<? $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");

$count = count($namesArray); ?>
$count值將為7。

你可以對(duì)任何數(shù)組添加元素,無(wú)論是在已存在數(shù)組的開(kāi)始或末尾。你也可以使用函數(shù)以創(chuàng)建一個(gè)包含兩個(gè)或多個(gè)數(shù)組元素的新數(shù)組。合并時(shí)每個(gè)數(shù)組將按需要的順序排列。如果你的數(shù)組已經(jīng)有內(nèi)部的排序,你需要對(duì)新的合并數(shù)組重排序。

讓我們從對(duì)已存在數(shù)組的末尾增添元素開(kāi)始,使用函數(shù)array_push():


<? /* 創(chuàng)建原始數(shù)組 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 加入到原始數(shù)組中 */

array_push($fruitArray, "grape", "pineapple", "tomato");

/* 通過(guò)其鍵值列出每個(gè)元素*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value<br>";

}

?>
這將顯示:

0 : apple

1 : orange

2 : banana

3 : kiwi

4 : pear

5 : grape

6 : pineapple

7 : tomato

當(dāng)你需要對(duì)數(shù)組開(kāi)頭添加元素時(shí),代碼非常類似。不同處只是函數(shù)名:array_unshift() 而不是array_push()。


<? /* 創(chuàng)建原始數(shù)組 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 加入到原始數(shù)組中 */

array_unshift($fruitArray, "grape", "pineapple", "tomato");

/* 通過(guò)其鍵值列出每個(gè)元素*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value<br>";

}

?>
這將顯示:

0 : grape

1 : pineapple

2 : tomato

3 : apple

4 : orange

5 : banana

6 : kiwi

7 : pear

函數(shù)array_merge()合并兩個(gè)或更多的數(shù)組。


<? /* 創(chuàng)建原始數(shù)組 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

<? /* 創(chuàng)建第二個(gè)數(shù)組 */

$vegArray = array("carrot", "green beans", "ASParagus", "artichoke", "corn");

/* 合并為一個(gè)數(shù)組 */

$goodfoodArray = array_merge($fruitArray, $vegArray);

/* 通過(guò)其鍵值列出每個(gè)元素*/

while (list($key,$value) = each($goodfoodArray)) {

echo "$key : $value<br>";

}

?>
這將顯示:

0 : apple

1 : orange

2 : banana

3 : kiwi

4 : pear

5 : carrot

6 : green beans

7 : ASParagus

8 : artichoke

9 : corn

現(xiàn)在已經(jīng)對(duì)數(shù)組進(jìn)行了增加元素和合并,現(xiàn)在來(lái)練習(xí)刪除元素函數(shù)。你可以使用函數(shù)array_pop()從一數(shù)組末尾刪除一個(gè)元素。如果使用函數(shù) array_shift(),則從一數(shù)組開(kāi)頭刪除一個(gè)元素。而實(shí)際上當(dāng)你從數(shù)組刪除元素時(shí),此元素對(duì)你而言仍然可用――當(dāng)你從已存在的數(shù)組中對(duì)元素進(jìn)行 pop 或 shift時(shí)。

使用array_pop()函數(shù)從數(shù)組末尾刪除一個(gè)值:


<?

/* 創(chuàng)建一數(shù)組*/

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 在末尾彈出某值 */

$popped = array_pop($fruitArray);

/* 列出新數(shù)組內(nèi)容,以及彈出的值*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value<br>";

}

echo "<br>and finally, in $popped: $popped";

?>

這將顯示:

0 : apple

1 : orange

2 : banana

3 : kiwi

and finally, in $popped: pear

Next, delete an element from the end of an array: ???????????

下面,從數(shù)組末尾刪除某值:


<?

/* 創(chuàng)建一數(shù)組*/

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 從數(shù)組頭部移出某值 */

$shifted = array_shift($fruitArray);

/* 列出新數(shù)組的內(nèi)容以及移出的值*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value<br>";

}

echo "<br>and finally, in $shifted: $shifted";

?>
這將顯示:

0 : orange

1 : banana

2 : kiwi

3 : pear

and finally, in $shifted: apple

有很多函數(shù)可以幫助你對(duì)數(shù)組元素排序。但我將會(huì)演示基本的排序以幫助你了解其過(guò)程:


<? /* 創(chuàng)建原始數(shù)組 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 排序 */

sort($fruitArray);

/* 對(duì)其重設(shè)以正確從頭到尾顯示數(shù)組 */

/* 通過(guò)其鍵值列出每個(gè)元素*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value<br>";

}

?>
這將顯示:

0 : apple

1 : banana

2 : kiwi

3 : orange

4 : pear


php技術(shù)PHP 數(shù)組實(shí)例說(shuō)明,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 拜城县| 彭山县| 山丹县| 韩城市| 宕昌县| 冷水江市| 河源市| 合水县| 阳朔县| 应用必备| 峨边| 安塞县| 荃湾区| 乳山市| 平阴县| 大理市| 泸溪县| 武隆县| 洱源县| 肇源县| 绥中县| 溧阳市| 郁南县| 利辛县| 五大连池市| 高陵县| 柯坪县| 崇信县| 岑巩县| 阳谷县| 巴里| 马尔康县| 庄河市| 鄱阳县| 黄梅县| 潢川县| 鄄城县| 乐都县| 屏边| 吉安县| 银川市|