亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

1.array_combine()

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

例子:
復制代碼 代碼如下:

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

print_r($c);
?>

上例將輸出:
復制代碼 代碼如下:

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


2.array_merge()

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

例子:
復制代碼 代碼如下:

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

上例將輸出:
復制代碼 代碼如下:

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


3.array_merge_recursive()

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

例子:
復制代碼 代碼如下:

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

上例將輸出 $result:
復制代碼 代碼如下:

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

[0] => blue
)

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

拆分數(shù)組有兩個函數(shù):

1.array_slice()

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

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

例子:
復制代碼 代碼如下:

<?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));
?>

上例將輸出:
復制代碼 代碼如下:

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

2.array_splice()

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

例子:
復制代碼 代碼如下:

<?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ù)有四個:

1.array_intersect()

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

例子:
復制代碼 代碼如下:

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

上例將輸出:
復制代碼 代碼如下:

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


2.array_intersect_assoc()

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

例子:
復制代碼 代碼如下:

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

上例將輸出:
復制代碼 代碼如下:

Array
(
[a] => green
)

3.array_diff()

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

例子:
復制代碼 代碼如下:

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

print_r($result);
?>

上例將輸出:
復制代碼 代碼如下:

Array
(
[1] => blue
)


4.array_diff_assoc()

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

例子:
復制代碼 代碼如下:

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

上例將輸出:
復制代碼 代碼如下:

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

相關文章

最新評論