php 數組的合并、拆分、區別存取子集_php技巧

來源:互聯網
上載者:User
合并數組有三個函數:

1.array_combine()

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

例子:
複製代碼 代碼如下:

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

攜帶兩個參數數組,簡單的將數組二追加到數組一的後面構成新數組。

例子:
複製代碼 代碼如下:

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

與上面函數雷同,唯一的區別是在追加時發現要添加的鍵已存在時,array_merge()的處理方式是覆蓋前面的索引值,array_merge_recursive()的處理方式是重構子數組,將重複的鍵的值組成一個新的數值數組。

例子:
複製代碼 代碼如下:

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

拆分數組有兩個函數:

1.array_slice()

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

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

例子:
複製代碼 代碼如下:

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

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

例子:
複製代碼 代碼如下:

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


區別存取子有四個:

1.array_intersect()

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

例子:
複製代碼 代碼如下:

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

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

例子:
複製代碼 代碼如下:

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

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

例子:
複製代碼 代碼如下:

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

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

例子:
複製代碼 代碼如下:

<?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
)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.