# php 好用的涵式
10個
https://www.jianshu.com/p/3e47bf8e2c80
## file_get_contents
api很常用到
```
$url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=yourClientID&client_secret=yourClientSecret&fb_exchange_token=shortLivedToken;
return json_decode(file_get_contents($url), true);
```
将文件的内容读入到一个字符串中的首选方法
## json_decode
`json_decode();`
這是一個將json格式轉換回物件或陣列的指令
`json_decode($json字串, ($assoc));`
$json字串:這裡我們要輸入的就是要解碼的json格式字串
$assoc:(選填)這個值預設是False,當這個值變成True的時候輸出的格式會從物件(Object)變為陣列(Array)
## func_get_args
獲取參數 然後放到一個array裡面
這要在func裡面才能用喔
## call_user_func和call_user_func_array区别
### 普通函数调用
```
<?php
function func($a, $b){
echo $a."\r\n";
echo $b."\r\n";
}
call_user_func('func', 1, 2);//第一个是函数名,后面是参数列表
call_user_func_array("func", array(3, 4));//区别于call_user_func只是参数传递的方式不同
```
输出:
```
1
2
3
4
```
### 调用类的静态方法与实例方法
```
<?php
class A {
function b($b) {
echo $b."\r\n";
}
static function c($c) {
echo $c."\r\n";
}
}
//在php5.3 之前是可以如下调用非静态函数的,系统会把b当做静态函数处理
// call_user_func(array("A", "b"), "b");
//在 php5.3 之后如果调用类的实例方法,必须通过类的实例调用,如下
$a = new A;
call_user_func(array($a, "b"), "b");
// 类的静态方法是可以通过如下两种方式调用的
call_user_func(array("A", "c"), "c");
call_user_func("A::c", "c");
//call_user_func_array 调用类的静态函数和实例函数
call_user_func_array(array($a, "b"), array("bb"));
call_user_func_array("A::c", array("cc"));
```
输出:
```
b
c
c
bb
cc
```
Service就用到了
```
public function __call($method, $arguments)
{
if (method_exists($this->repository, $method)) {
return call_user_func_array([$this->repository, $method], $arguments);
}
}
```
## query String
http_build_query
## array解構
陣列的解構
https://www.w3school.com.cn/php/func_array_list.asp
https://learnku.com/articles/48963
list 仅适用于数值数组,并假设数字索引从 0 开始。
可能适合您的目的是将 extract () 变量从数组导入当前符号表的函数。
虽然 list 您可以明确定义变量名称,extract () 但不会给您这种自由。
提取关联数组
有了 extract 你可以做这样的事情:
```
<?php
$info = [ 'drink' => 'coffee', 'color' => 'brown', 'power' => 'caffeine' ];
extract($info);
var_dump($drink); // string(6) "coffee"
var_dump($color); // string(5) "brown"
var_dump($power); // string(8) "caffeine"
```
Symmetric array destructuring
短数组语法([])现在作为 list() 语法的一个备选项,可以用于将数组的值赋给一些变量(包括在 foreach 中)。
```
<?php
$data = [
[1, 'Tom'],
[2, 'Fred']
];
// 使用 list()
list($id1, $name1) = $data[0];
foreach($data as list($ids, $name)) {
}
// 使用 []
[$id1, $name1] = $data[0];
foreach($data as [$id, $name]) {
}
```
list () 现在支持键名
现在 list() 和它的新的 [] 语法支持在它内部指定键名。
## 多异常捕获处理
一个 catch 语句块现在可以通过管道字符 (/)来实现多个异常的捕获。
```
<?php
try {
} catch (FirstException | SecondException $e) {
}
```
## ucwords ucfirst
把每个单词的首字符转换为大写:
```
<?php
echo ucwords("hello world");
?>
```
## impolde 跟 array_keys
array_keys可以把你的key拿出來
implode把array合在一起
這樣就能用in去比對
```
namespacs App\model\;
public const ACTIVEITY_PREPARE = 0;
public const ACTIVEITY_CONDUCTING = 1;
public const ACTIVEITY_END = 2;
public const ACTIVEITY_STATUS = [
self::ACTIVEITY_PREPARE => '準備中',
self::ACTIVEITY_CONDUCTING => '進行中',
self::ACTIVEITY_END => '已結束',
];
```
```
namespace App\Http\Request
return parent::rules() + [
'name' => 'sometimes|nullable|string|max:255',
'prize_type' => 'sometimes|nullable|in:' . implode(',', array_keys(Task::PRIZE_TYPE)),
'activeity_status' => 'sometimes|nullable|in:' . implode(',', array_keys(TASK::ACTIVEITY_STATUS)),
];
```
## array_column
他會取key
跟array_keys不一樣 這是會取所有key
## PHP array_merge與 陣列相加 '+' 比較
http://blog.hsatac.net/2012/11/php-array-plus-array-versus-array-merge/
array_merge
key一樣會後面蓋掉前面
如果沒有key的值, 則會附加在陣列尾端, 且index會重新排序
array+array的情況, 行為則是相反
會前蓋後, 沒有key的值也會前蓋後, 且index不會重新排序
性能方面
是+號比較好
但還是看情況比較好
## sprintf
可以插入多個

把東西插入
比起單純雙引號號 這樣%s 這個s是string好分辨
而且可替換
```
public function store(FAQStoreRequest $request)
{
$attributes = $request->validated();
$attributes['plain_text_content'] = strip_tags($attributes['content']);
$faq = $this->service->create($attributes);
return redirect($this->getIndexRoute())
->with(
$this->alertSuccess(sprintf('已新增 %s', $faq->title))
);
}
```
## strip_tags
看上面的
這是把html 跟其他空白刪除
這邊前端是sunnote那種 所見及所得的,所以她可以把它去掉,原因他圖片之類的都在前端救上傳了
```
public function store(TaskStoreRequest $request)
{
$attributes = $request->validated();
$attributes['plain_text_content'] = strip_tags('content');
$task = $this->service->create($attributes);
return redirect()
->route('admin.task.index')
->with('success', sprintf('已新增 %s', $task->titlt));
}
```
## rtrim
刪除指定的
rtrim() 函數從字符串的末端開始刪除空白字符或其他預定義字符。
## array_search
array_search(); 主要的功能除了查詢是否存在外,它會回傳搜尋到的KEY值,意味著你能夠利用這個KEY值直接套用到你的程式中,可以直接使用這個搜尋到的變數,它的使用方法也相當的簡單。
array_search(要搜尋的值,被搜尋的陣列);
他的兄弟
**in_array()**; 主要的功能在於查詢是否存在,若存在則回傳TRUE,若是不存在則回傳FLASE,它單純是一個用來判斷條件是否純在陣列的功能,而且使用的方法也相當的簡單。
/**
* 修改排序.
*/
public function formSort(array $ids): void
{
$afterArray = $this->repository->getWhereInBy($ids);
foreach ($afterArray as $item) {
$key = array_search($item->id, $ids);
if ($key > -1) {
$item->sort = $key + 1;
$item->save();
}
}
}
## func_get_args()
會去抓傳進來的參數
實際用到在model all()
```
/**
* Get all of the models from the database.
*
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public static function all($columns = ['*'])
{
return static::query()->get(
is_array($columns) ? $columns : func_get_args()
);
}
```
###### tags: `PHP`