#แบบที่ 1
1 |
$output = $array1 + $array2; |
#แบบที่ 2
1 |
$output = array_merge($array1, $array2); |
ตัวอย่าง แบบที่ 1 : $array1 + $array2; key เหมือนกันจะดึงมาแค่ 1 เรคคอร์ดแรก
1 2 3 4 |
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a'); $array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b'); $result = $array1 + $array2; var_dump($result); |
ผลลัพธ์
1 2 3 4 5 6 7 8 9 10 11 12 |
array(5) { [0]=> string(6) "zero_a" [2]=> string(5) "two_a" [3]=> string(7) "three_a" [1]=> string(5) "one_b" [4]=> string(6) "four_b" } |
ตัวอย่าง แบบที่ 2 : array_merge($array1, $array2);
1 2 3 4 |
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a'); $array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b'); $result = array_merge($array1, $array2); var_dump($result); |
ผลลัพธ์
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
array(6) { [0]=> string(6) "zero_a" [1]=> string(5) "two_a" [2]=> string(7) "three_a" [3]=> string(5) "one_b" [4]=> string(7) "three_b" [5]=> string(6) "four_b" } |
1 |
array_flip ( array $array ) : array |
ตัวอย่าง
1 2 3 |
$array1 = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $result = array_flip($array1); var_dump($result); |
ผลลัพธ์
1 2 3 4 5 6 7 8 9 10 |
array(4) { ["red"]=> string(1) "a" ["green"]=> string(1) "b" ["blue"]=> string(1) "c" ["yellow"]=> string(1) "d" } |
1 |
array_keys ( array $array ) : array |
ตัวอย่าง
1 2 |
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a)); |
ผลลัพธ์
1 |
Array ( [0] => Volvo [1] => BMW [2] => Toyota ) |
sort()
– sort arrays in ascending orderrsort()
– sort arrays in descending orderasort()
– sort associative arrays in ascending order, according to the valueksort()
– sort associative arrays in ascending order, according to the keyarsort()
– sort associative arrays in descending order, according to the valuekrsort()
– sort associative arrays in descending order, according to the keyFunction name | Sorts by | Maintains key association | Order of sort | Related functions |
---|---|---|---|---|
array_multisort() | value | associative yes, numeric no | first array or sort options | array_walk() |
asort() | value | yes | low to high | arsort() |
arsort() | value | yes | high to low | asort() |
krsort() | key | yes | high to low | ksort() |
ksort() | key | yes | low to high | asort() |
natcasesort() | value | yes | natural, case insensitive | natsort() |
natsort() | value | yes | natural | natcasesort() |
rsort() | value | no | high to low | sort() |
shuffle() | value | no | random | array_rand() |
sort() | value | no | low to high | rsort() |
uasort() | value | yes | user defined | uksort() |
uksort() | key | yes | user defined | uasort() |
usort() | value | no | user defined | uasort() |
โดยจะคืนค่า key แรกที่พบ
1 |
array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : mixed |
ตัวอย่าง
1 2 3 4 |
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key = array_search('red', $array); // $key = 1; |
1 |
array_unique ( array $array [, int $sort_flags = SORT_STRING ] ) : array |
ตัวอย่าง
1 2 3 |
$input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); |
ผลลัพธ์
1 2 3 4 5 6 |
Array ( [a] => green [0] => red [1] => blue ) |
1 2 3 4 5 |
min(array_values); or min(value1,value2,...); |
ตัวอย่าง
1 2 3 4 5 6 7 8 9 |
echo(min(2,4,6,8,10) . "<br>"); echo(min(22,14,68,18,15) . "<br>"); echo(min(array(4,6,8,10)) . "<br>"); echo(min(array(44,16,81,12))); //2 //14 //4 //12 |
1 |
implode(separator,array) |
ตัวอย่าง
1 2 3 4 |
$arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr); //Hello World! Beautiful Day! |
1 |
explode(separator,string,limit) |
ตัวอย่าง
1 2 3 4 |
$str = "Hello world. It's a beautiful day."; print_r (explode(" ",$str)); //Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. ) |
1 |
in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool |
ตัวอย่าง
1 2 3 4 5 6 7 8 9 10 11 12 |
$people = array("Peter", "Joe", "Glenn", "Cleveland"); if (in_array("Glenn", $people)) { echo "Match found"; } else { echo "Match not found"; } //Match found |
ดูเพิ่มเติม
https://www.w3schools.com/php/php_arrays.asp
https://www.php.net/manual/en/array.sorting.php