---
tags: Laravel
---
# PHP常用的陣列相關函式
### ==array_column==
> 取得陣列的特定的值
- array_column(第一個參數為變數,第二個是要指定的key)
```php=
$row = [
['id' => 1, 'name' => 'bryan'],
['id' => 2, 'name' => 'john'],
['id' => 3, 'name' => 'lala'],
];
// 第一個參數為變數,第二個是要指定的key
array_column($row, 'name');
print_r(array_column($row, 'id'));
//顯示
Array
(
[0] => 1
[1] => 2
[2] => 3
)
```
### ==array_keys 獲取陣列 key==
* 印出array key
```php=
$row = [
['id' => 1, 'name' => 'bryan'],
['id' => 2, 'name' => 'john'],
['id' => 3, 'name' => 'lala'],
];
// 印出 陣列變數第0個的"key"
print_r(array_keys($row[0]));
//顯示
Array
(
[0] => id
[1] => name
)
```
### ==array_values 獲取出列的值values==
```php=
$row = [
['id' => 1, 'name' => 'bryan'],
['id' => 2, 'name' => 'john'],
['id' => 3, 'name' => 'lala'],
];
array_column($row, 'name');
print_r(array_values($row[0]));
//顯示
Array
(
[0] => 1
[1] => bryan
)
```
### ==array_map 從新組新陣列==
* 當我們的陣列有需要從組陣列內容時、就需要用到 array_map ,讓他從新去從組陣列
1. 從組陣列我們先賦予一個新的變數。
2. array_map給予一個function。
3. 給予的參數有點像似 foreach中給予的值 :foreach ($row as $newlist)。
4. 內容將要舊的取代新的陣列從組。
5. return 一個新的陣列出來。
```php=
$row = [
['id' => 1, 'name' => 'bryan'],
['id' => 2, 'name' => 'john'],
['id' => 3, 'name' => 'lala'],
];
// 從組陣列我們先賦予一個新的變數
// 類似 foreach ($row as $newlist)
$newarray = array_map(function ($newlist) {
$newlist['id'] = $newlist['id'] * 2;
return $newlist;
}, $row);
print_r($newarray);
//顯示
Array
(
[0] => Array
(
[id] => 2
[name] => bryan
)
[1] => Array
(
[id] => 4
[name] => john
)
[2] => Array
(
[id] => 6
[name] => lala
)
)
```
### ==array_filter 篩選需要的內容==
```php=
$row = [
['id' => 1, 'name' => 'bryan'],
['id' => 2, 'name' => 'john'],
['id' => 3, 'name' => 'lala'],
];
$newArray = array_filter($row, function ($newList) {
// 篩選 符合條件的內容
return $newList['id'] >= 2;
});
print_r($newArray);
//顯示
Array
(
[1] => Array
(
[id] => 2
[name] => john
)
[2] => Array
(
[id] => 3
[name] => lala
)
)
```
### ==array_push 新增陣列值==
```php=
$row = [1, 2, 3, 4, 5];
// 第一個為變數、第二個為新增的值
array_push($row, 8);
print_r($row);
//顯示
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 8
)
```
### ==array_pop 刪除最後一個值==
```php=
$row = [1, 2, 3, 4, 5];
array_pop($row);
print_r($row);
```
### ==合併陣列==
* 將陣列組合 合併
```php=
// 合併 陣列
$row = ["name" => "bryan", 'age' => 10, 'username' => 'Mars'];
$row = array_merge($row, ['name' => 'dd', 'id' => 88]);
print_r($row);
```
### ==sort 重整排序==
```php=
$row = [4, 5, 6, 8, 9, 2, 1];
sort($row);
print_r($row);
//顯示
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
[4] => 6
[5] => 8
[6] => 9
)
```
### ==asort 依照value排序==
```php=
$row = ["a" => 'bryan', "c" => 'list', "h" => 'mars', "y" => 'allen', "s" => 'danny',];
asort($row);
print_r($row);
//顯示
Array
(
[y] => allen
[a] => bryan
[s] => danny
[c] => list
[h] => mars
)
```
### ==ksort 依照 key排序==
```php=
$row = ["a" => 'bryan', "c" => 'list', "h" => 'mars', "y" => 'allen', "s" => 'danny',];
ksort($row);
print_r($row);
//顯示
Array
(
[a] => bryan
[c] => list
[h] => mars
[s] => danny
[y] => allen
)
```
### ==array_search 搜尋==
```php
$row = ['bryan', 'list', 'mars', 'allen', 'danny'];
echo (array_search('list', $row));
//顯示 key位置
1
```
### ==array_unique 過濾重複==
```php=
$row = ['bryan', 'list', 'danny', 'allen', 'danny'];
//過濾重複 只留最後一個值
array_unique($row);
print_r($row);
//顯示
Array
(
[0] => bryan
[1] => list
[2] => danny
[3] => allen
[4] => danny
)
```
### ==array_sum 總數==
```php=
$row=[3,5,10,12,200];
echo(array_sum($row));
//顯示
230
```
### ==count 計算array總數==
```php=
$row=[3,5,10,12,200];
echo(count($row));
//顯示
5
```