在本文中我們使用 print_r
來印出 array 的結構,方便驗證我們的 code
$arr = ['a', 'b', 'c'];
print_r($arr);
output
Array
(
[0] => a
[1] => b
[2] => c
)
sum
就只是取 value 的 總和
$nums = [4, 3, 5, 2, 1];
$sum = array_sum($nums);
print_r($sum);
output
15
count
就只是取 array 的個數
$nums = [4, 3, 5, 2, 1];
$sum = count($nums);
print_r($sum);
output
5
array_column
將 array 中指定的 key 當中的 value 取出成一個新的 array
$records = [
["id" => 1, "name" => "dad", "age" => 34],
["id" => 2, "name" => "son", "age" => 14],
["id" => 3, "name" => "mom", "age" => 28],
];
$column = array_column($records, 'name');
print_r($column);
output
Array
(
[0] => dad
[1] => son
[2] => mom
)
array_keys
將 array 中的 key 取出成一個新的 array
$records = ["id" => 1, "name" => "dad", "age" => 34];
$keys = array_keys($records);
print_r($keys);
output
Array
(
[0] => id
[1] => name
[2] => age
)
array_values
將 array 中的 value 取出成一個新的 array
$records = ["id" => 1, "name" => "dad", "age" => 34];
$values = array_values($records);
print_r($values);
output
Array
(
[0] => 1
[1] => dad
[2] => 34
)
array_map
根據當前的 array 資料產生新的 array
$records = [
["id" => 1, "name" => "dad", "age" => 34],
["id" => 2, "name" => "son", "age" => 14],
["id" => 3, "name" => "mom", "age" => 28],
];
$map = array_map(
function($item) {
$item['rank'] = $item['id'] * 5;
return $item;
},
$records
);
print_r($map);
output
Array
(
[0] => Array
(
[id] => 1
[name] => dad
[age] => 34
[rank] => 5
)
[1] => Array
(
[id] => 2
[name] => son
[age] => 14
[rank] => 10
)
[2] => Array
(
[id] => 3
[name] => mom
[age] => 28
[rank] => 15
)
)
array_filter
根據當前的 array 進行篩選
$records = [
["id" => 1, "name" => "dad", "age" => 34],
["id" => 2, "name" => "son", "age" => 14],
["id" => 3, "name" => "mom", "age" => 28],
];
$filter = array_filter(
$records,
function($item) {
return $item['age'] > 25;
}
);
print_r($filter);
output
Array
(
[0] => Array
(
[id] => 1
[name] => dad
[age] => 34
)
[2] => Array
(
[id] => 3
[name] => mom
[age] => 28
)
)
array_push
新增一個 item 於 array 末端
$nums = [5, 6, 7];
array_push($nums, 8);
print_r($nums);
output
Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
array_pop
移除 array 最末端的 item
$nums = [5, 6, 7];
array_pop($nums);
print_r($nums);
output
Array
(
[0] => 5
[1] => 6
)
array_merge
進行 array 的 合併和 value 複寫
Example 1
$arr1 = ['a', 'b', 'c'];
$arr2 = ['d', 'e', 'f'];
$arr3 = array_merge($arr1, $arr2);
print_r($arr3);
output
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
Example 2
$arr = ['id' => 1, "name" => "wang"];
$arr2 = array_merge($arr, ["name" => "aki", "age" => 34]);
print_r($arr2);
output
Array
(
[id] => 1
[name] => aki
[age] => 34
)
sort
依照 value 由小到大排序
$nums = [4, 3, 5, 6, 2, 1];
sort($nums);
print_r($nums);
output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
asort
依照 value 由小到大排序,但 key 保留原本的排序
$nums = [4, 3, 5, 6, 2, 1];
asort($nums);
print_r($nums);
註:value 1 在 asort
前在 index 5 的位置,因此 asort
完 key 為 5
註:value 2 在 asort
前在 index 4 的位置,因此 asort
完 key 為 4
output
Array
(
[5] => 1
[4] => 2
[1] => 3
[0] => 4
[2] => 5
[3] => 6
)
ksort
依照 key 由小到大排序
$nums = ["d" => 4, "c" => 3, "e" => 5, "f" => 6, "b" => 2, "a" => 1];
ksort($nums);
print_r($nums);
output
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
)
array_search
找出 value 在 array 中的 index,與 Java 中的 indexOf 功能相同
$arr = ['a', 'b', 'c'];
$index = array_search('b', $arr);
print_r($index);
output
1
array_unique
去除重複的 value
$arr = ['⬆', '⬆', '⬇', '⬇', '⬅', '⮕', '⬅', '⮕', 'B', 'A'];
$arr2 = array_unique($arr);
print_r($arr2);
output
Array
(
[0] => ⬆
[2] => ⬇
[4] => ⬅
[5] => ⮕
[8] => B
[9] => A
)