最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php 數(shù)組的合并、拆分、區(qū)別取值函數(shù)集

 更新時(shí)間:2010年02月15日 16:06:51   作者:  
都說PHP的數(shù)組功能很強(qiáng)大、只有真正用于項(xiàng)目工作當(dāng)中才能夠感受得到,至少我認(rèn)為是,現(xiàn)在已慢慢的發(fā)覺其中的奧秘了……
合并數(shù)組有三個(gè)函數(shù):

1.array_combine()

攜帶兩個(gè)參數(shù)數(shù)組,參數(shù)數(shù)組一的值作新數(shù)組的鍵,參數(shù)數(shù)組二的值作新數(shù)組的值。很簡(jiǎn)單。

例子:
復(fù)制代碼 代碼如下:

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

上例將輸出:
復(fù)制代碼 代碼如下:

Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)


2.array_merge()

攜帶兩個(gè)參數(shù)數(shù)組,簡(jiǎn)單的將數(shù)組二追加到數(shù)組一的后面構(gòu)成新數(shù)組。

例子:
復(fù)制代碼 代碼如下:

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

上例將輸出:
復(fù)制代碼 代碼如下:

Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)


3.array_merge_recursive()

與上面函數(shù)雷同,唯一的區(qū)別是在追加時(shí)發(fā)現(xiàn)要添加的鍵已存在時(shí),array_merge()的處理方式是覆蓋前面的鍵值,array_merge_recursive()的處理方式是重構(gòu)子數(shù)組,將重復(fù)的鍵的值組成一個(gè)新的數(shù)值數(shù)組。

例子:
復(fù)制代碼 代碼如下:

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
?>

上例將輸出 $result:
復(fù)制代碼 代碼如下:

Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)

[0] => blue
)

[0] => 5
[1] => 10
)

拆分?jǐn)?shù)組有兩個(gè)函數(shù):

1.array_slice()

攜帶三個(gè)參數(shù),參數(shù)一為目標(biāo)數(shù)組,參數(shù)二為offset,參數(shù)三為length。作用為,從目標(biāo)數(shù)組中取出從offset開始長(zhǎng)度為length的子數(shù)組。

如果offset為正數(shù),則開始位置從數(shù)組開頭查offset處,如果offset為負(fù)數(shù)開始位置從距數(shù)組末尾查offset處。如果length為正數(shù),則毫無疑問取出的子數(shù)組元素個(gè)數(shù)為length,如果length為負(fù)數(shù),則子數(shù)組從offset開始到距數(shù)組開頭count(目標(biāo)數(shù)組)-|length|處結(jié)束。特殊地,如果length為空,則結(jié)束位置在數(shù)組結(jié)尾。

例子:
復(fù)制代碼 代碼如下:

<?php
$input = array("a", "b", "c", "d", "e");

$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"

// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

上例將輸出:
復(fù)制代碼 代碼如下:

Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)

2.array_splice()

攜帶三個(gè)參數(shù),同上,作用是刪除從offset開始長(zhǎng)度為length的子數(shù)組。

例子:
復(fù)制代碼 代碼如下:

<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>


區(qū)別取值函數(shù)有四個(gè):

1.array_intersect()

攜帶參數(shù)不定,均為數(shù)組,返回所有數(shù)組中公共元素的值組成的數(shù)組,數(shù)組的鍵由所在第一個(gè)數(shù)組的鍵給出。

例子:
復(fù)制代碼 代碼如下:

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>

上例將輸出:
復(fù)制代碼 代碼如下:

Array
(
[a] => green
[0] => red
)


2.array_intersect_assoc()

在前一個(gè)函數(shù)的基礎(chǔ)上,返回所有數(shù)組中鍵、值均相同的鍵值對(duì)。

例子:
復(fù)制代碼 代碼如下:

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
?>

上例將輸出:
復(fù)制代碼 代碼如下:

Array
(
[a] => green
)

3.array_diff()

攜帶多個(gè)數(shù)組,返回第一個(gè)數(shù)組中有的而后面的數(shù)組中沒有的所有的值組成的新數(shù)組,對(duì)應(yīng)鍵取自第一個(gè)數(shù)組。

例子:
復(fù)制代碼 代碼如下:

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

上例將輸出:
復(fù)制代碼 代碼如下:

Array
(
[1] => blue
)


4.array_diff_assoc()

在前一個(gè)函數(shù)的基礎(chǔ)上,不僅需要匹配值還要匹配鍵。

例子:
復(fù)制代碼 代碼如下:

<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
?>

上例將輸出:
復(fù)制代碼 代碼如下:

Array
(
[b] => brown
[c] => blue
[0] => red
)

相關(guān)文章

最新評(píng)論

界首市| 洛川县| 佳木斯市| 商洛市| 平顶山市| 开鲁县| 柳河县| 东源县| 来宾市| 监利县| 章丘市| 天门市| 新昌县| 巨野县| 咸丰县| 巴马| 略阳县| 胶南市| 赤城县| 甘孜县| 南安市| 潼南县| 瑞丽市| 双桥区| 鹤庆县| 鄂伦春自治旗| 大名县| 黄冈市| 思茅市| 莒南县| 宜州市| 巴中市| 绵竹市| 普格县| 泾源县| 威信县| 涿鹿县| 柳河县| 棋牌| 桐乡市| 宿迁市|