# Introduction
學習一個框架, Ray 的想法是, 在深入理解底層實作的原理之前, 應該先知道這個框架的 使用方法
; 先學習怎麼使用這個前人造的輪子, 再學習怎麼樣造一個輪子。
所以本篇文章重點在於細讀官方文件, 並將內容理解後以 Q&A 的方式記錄下來, 加速學習以及查詢。
# Method Listing
# Arrays & Objects
# Arr::accessible()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
$isAccessible = Arr::accessible(['a' => 1, 'b' => 2]);
$isAccessible = Arr::accessible(new Collection);
$isAccessible = Arr::accessible('abc');
$isAccessible = Arr::accessible(new stdClass);Answer:
判斷 parameter 是否 array accessible- Output:
<?php
// true
// true
// false
// false
# Arr::add()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = Arr::add(['name' => 'Desk'], 'price', 100);
$array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);Answer:
將 key / value 加到 arg1 array
如果 given key 不存在於該 array, 或存在但其 value 為 null- Output:
<?php
// ['name' => 'Desk', 'price' => 100]
// ['name' => 'Desk', 'price' => 100]
# Arr::collapse()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);Answer:
將含有多個 arrays 的 array 變成一個 single array- Output:
<?php
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Arr::crossJoin()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$matrix = Arr::crossJoin([1, 2], ['a', 'b']);
$matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']);Answer:
cross join 帶入的 arrayOutput:
<?php
/*
[
[1, 'a'],
[1, 'b'],
[2, 'a'],
[2, 'b'],
]
*/
/*
[
[1, 'a', 'I'],
[1, 'a', 'II'],
[1, 'b', 'I'],
[1, 'b', 'II'],
[2, 'a', 'I'],
[2, 'a', 'II'],
[2, 'b', 'I'],
[2, 'b', 'II'],
]
*/
# Arr::divide()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
[$keys, $values] = Arr::divide(['name' => 'Desk']);Answer:
返回兩個 arrays, 一個裝 key, 一個裝 valueOutput:
<?php
// $keys: ['name']
// $values: ['Desk']
# Arr::dot()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$flattened = Arr::dot($array);Answer:
將一個 multi-dimensional array 變成一個 single array, 並使用. (英文句點符號)
來表示 depth- Output:
<?php
// ['products.desk.price' => 100]
# Arr::except()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$filtered = Arr::except($array, ['price']);Answer:
將指定於 arg2 的 key 從 arg1 array 中移除- Output:
<?php
// ['name' => 'Desk']
# Arr::exists()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'John Doe', 'age' => 17];
$exists = Arr::exists($array, 'name');
$exists = Arr::exists($array, 'salary');Answer:
判斷 arg2 的 key 是否存在於 arg1 array- Output:
<?php
// true
// false
# Array::first()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = [100, 200, 300];
$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});Answer:
回傳 arg1 array 中第一個符合 arg2 closure 中邏輯的輸出- Output:
<?php
// 200
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$first = Arr::first($array, $callback, $default);Answer:
回傳 arg1 array 中第一個符合 arg2 closure 中邏輯的輸出, 如果 array 中沒有出現符合的 item, 則 return arg3 $default
# Arr::flatten()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$flattened = Arr::flatten($array);Answer:
將一個 multi-dimensional array 變為一個 single array- Output:
<?php
// ['Joe', 'PHP', 'Ruby']
# Arr::forget()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
Arr::forget($array, 'products.desk');Answer:
將 arg2 指定 key 從 arg1 $array 中移除
可使用. (英文句點符號)
來表示 depth- Output:
<?php
// ['products' => []]
# Arr::get()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$price = Arr::get($array, 'products.desk.price');Answer:
從 arg1 $array 中取得 arg2 指定 key 的 value
可使用. (英文句點符號)
來表示 depth- Output:
<?php
// 100
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$discount = Arr::get($array, 'products.desk.discount', 0);Answer:
從 arg1 $array 中取得 arg2 指定 key 的 value
可使用. (英文句點符號)
來表示 depth
如果沒有指定 key, 可指定 default 值- Output:
<?php
// 0
# Arr::has()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
$contains = Arr::has($array, 'product.name');
$contains = Arr::has($array, ['product.price', 'product.discount']);Answer:
判斷 arg2 指定 key 是否存在於 arg1 $array
可使用. (英文句點符號)
來表示 depth- Output:
<?php
// true
// false
# Arr::hasAny()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
$contains = Arr::hasAny($array, 'product.name');
$contains = Arr::hasAny($array, ['product.name', 'product.discount']);
$contains = Arr::hasAny($array, ['category', 'product.discount']);Answer:
判斷 arg2 指定的的多個 item 是否有任何一個存在於 arg1 $array- Output:
<?php
// true
// true
// false
# Arr::isAssoc()
以下的 Laravel example code 的意思是?
Example:
<?php
use Illuminate\Support\Arr;
$isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]);
$isAssoc = Arr::isAssoc([1, 2, 3]);Answer:
判斷帶入的 array 是否為一個 associative array
如果沒有從 0 開始的 sequential number 作為 key 的 array 都視為 associative array- Output:
<?php
// true
// false
留言