ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## think-helper 常用的一些扩展类库 > 以下类库都在`\\think\\helper`命名空间下 ***** ## 数组 & 对象 要使用数组类,首先需要在你的类文件中引入 ~~~ use think\helper\Arr; ~~~ ***** #### `Arr::add($array, $key, $value)` 如果给定的键在数组中不存在或数组被设置为`null`,那么 `Arr::add` 函数将会把给定的键值对添加到数组中: ``` use think\helper\Arr; $array = Arr::add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100] $array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100); // ['name' => 'Desk', 'price' => 100] ``` ***** #### `Arr::collapse($array)` 函数将多个数组合并为一个数组 ``` use think\helper\Arr; $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9] ``` ***** #### `Arr::divide($array)` 函数返回一个二维数组,一个值包含原始数组的键,另一个值包含原始数组的值: ``` use think\helper\Arr; [$keys, $values] = Arr::divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk'] ``` ***** #### `Arr::dot($array, $prepend = '')` 函数将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系: ``` use think\helper\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $flattened = Arr::dot($array); // ['products.desk.price' => 100] ``` ***** #### `Arr::except($array, $keys)` 函数从数组中删除指定的键值对: ``` use think\helper\Arr; $array = ['name' => 'Desk', 'price' => 100]; $filtered = Arr::except($array, ['price']); // ['name' => 'Desk'] ``` ***** #### `Arr::first($array, callable $callback = null, $default = null)` 函数返回数组中通过真值测试的第一个元素: ``` use think\helper\Arr; $array = [100, 200, 300]; $first = Arr::first($array, function ($value, $key) { return $value >= 150; }); // 200 ``` ***** #### `Arr::forget(&$array, $keys)` 函数使用「.」符号从深度嵌套的数组中删除给定的键值对: ``` use think\helper\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.desk'); // ['products' => []] ``` ***** #### `Arr::get($array, $key, $default = null)` 函数使用「.」符号从深度嵌套的数组中根据指定键检索值: ``` use think\helper\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $price = Arr::get($array, 'products.desk.price'); // 100 ``` ***** #### `Arr::has($array, $keys)` 函数使用「.」符号查找数组中是否存在指定的一个或多个键: ``` use think\helper\Arr; $array = ['product' => ['name' => 'Desk', 'price' => 100]]; $contains = Arr::has($array, 'product.name'); // 真 $contains = Arr::has($array, ['product.price', 'product.discount']); // 假 ``` ***** #### `Arr::last($array, callable $callback = null, $default = null)` 函数返回数组中通过指定测试的最后一个元素: ``` use think\helper\Arr; $array = [100, 200, 300, 110]; $last = Arr::last($array, function ($value, $key) { return $value >= 150; }); // 300 ``` ***** #### `Arr::only($array, $keys)` 函数只返回给定数组中指定的键值对: ``` use think\helper\Arr; $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = Arr::only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100] ``` ***** #### `Arr::pluck($array, $value, $key = null)` 函数从数组中检索给定键的所有值: ``` use think\helper\Arr; $array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $names = Arr::pluck($array, 'developer.name'); // ['Taylor', 'Abigail'] ``` ***** #### `Arr::prepend($array, $value, $key = null)` 函数将一个值插入到数组的开始位置: ``` use think\helper\Arr; $array = ['one', 'two', 'three', 'four']; $array = Arr::prepend($array, 'zero'); // ['zero', 'one', 'two', 'three', 'four'] ``` 如果需要,你可以指定你插入值的键: ``` use think\helper\Arr; $array = ['price' => 100]; $array = Arr::prepend($array, 'Desk', 'name'); // ['name' => 'Desk', 'price' => 100] ``` ***** #### `Arr::pull(&$array, $key, $default = null)` 函数从数组中返回指定键的值并删除此键/值对: ``` use think\helper\Arr; $array = ['name' => 'Desk', 'price' => 100]; $name = Arr::pull($array, 'name'); // $name: Desk // $array: ['price' => 100] ``` ***** #### `Arr::random($array, $number = null)` 函数从数组中随机返回一个值: ``` use think\helper\Arr; $array = [1, 2, 3, 4, 5]; $random = Arr::random($array); // 4 - (随机检索) ``` ***** #### `Arr::set(&$array, $key, $value)` 函数使用「.」符号在多维数组中设置指定键的值: ``` use think\helper\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]] ``` ***** #### `Arr::sort($array, $callback = null)` 函数根据数组的值对数组进行排序: ``` use think\helper\Arr; $array = ['Desk', 'Table', 'Chair']; $sorted = Arr::sort($array); // ['Chair', 'Desk', 'Table'] ``` 你也可以根据给定闭包返回的结果对数组进行排序: ``` use think\helper\Arr; $array = [ ['name' => 'Desk'], ['name' => 'Table'], ['name' => 'Chair'], ]; $sorted = array_values(Arr::sort($array, function ($value) { return $value['name']; })); /* [ ['name' => 'Chair'], ['name' => 'Desk'], ['name' => 'Table'], ] */ ``` ***** #### `Arr::sortRecursive($array)` 函数使用 `sort` 函数对数值子数组进行递归排序,使用`ksort`函数对关联子数组进行递归排序: ``` use think\helper\Arr; $array = [ ['Roman', 'Taylor', 'Li'], ['PHP', 'Ruby', 'JavaScript'], ['one' => 1, 'two' => 2, 'three' => 3], ]; $sorted = Arr::sortRecursive($array); /* [ ['JavaScript', 'PHP', 'Ruby'], ['one' => 1, 'three' => 3, 'two' => 2], ['Li', 'Roman', 'Taylor'], ] */ ``` ***** #### `Arr::where($array, callable $callback)` 函数使用给定闭包返回的结果过滤数组: ``` use think\helper\Arr; $array = [100, '200', 300, '400', 500]; $filtered = Arr::where($array, function ($value, $key) { return is_string($value); }); // [1 => '200', 3 => '400'] ``` ***** #### `Arr::wrap($value)` 函数将给定的值变为一个数组,如果给定的值已经是数组,则不改变: ``` use think\helper\Arr; $string = 'Laravel'; $array = Arr::wrap($string); // ['Laravel'] ``` ***** #### `data_fill(&$target, $key, $value)` 函数使用「.」符号在多维数组或对象内设置缺省值: ``` $data = ['products' => ['desk' => ['price' => 100]]]; data_fill($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 100]]] data_fill($data, 'products.desk.discount', 10); // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]] ``` 这个函数还接受星号「*」作为通配符,相应的填充目标: ``` $data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2'], ], ]; data_fill($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 200], ], ] */ ``` ***** #### `data_get($target, $key, $default = null)` 函数使用「.」符号从多维数组或对象中检索值 ``` $data = ['products' => ['desk' => ['price' => 100]]]; $price = data_get($data, 'products.desk.price'); // 100 ``` 这个函数还接受「*」作为通配符,它可以匹配数组或对象的任何键: ``` $data = [ 'product-one' => ['name' => 'Desk 1', 'price' => 100], 'product-two' => ['name' => 'Desk 2', 'price' => 150], ]; data_get($data, '*.name'); // ['Desk 1', 'Desk 2']; ``` ***** #### `data_set(&$target, $key, $value, $overwrite = true)` 函数使用「.」符号在多维数组或对象中设置一个值: ``` $data = ['products' => ['desk' => ['price' => 100]]]; data_set($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]] ``` 该函数也可以接收「*」通配符,相应的在指定键上设置值: ``` $data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 150], ], ]; data_set($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 200], ['name' => 'Desk 2', 'price' => 200], ], ] */ ``` ***** #### `class_basename($class)` 函数返回被删除了命名空间的指定类的类名: ``` $class = class_basename('Foo\Bar\Baz'); // Baz ``` ***** #### `class_uses_recursive($class)` 函数返回一个类使用的所有 traits , 包括它所有父类使用的 traits: ``` $traits = class_uses_recursive(App\User::class); ``` ***** #### `trait_uses_recursive($trait)` 返回被 trait 使用的全部 trait: ``` $traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class); ``` ***** #### `collect($value = null)` 函数根据给定的值创建一个collection实例: ``` $collection = collect(['taylor', 'abigail']); ``` ***** #### `value($value)` 函数返回给定值。如果传递`闭包`给此函数,将执行`闭包`并返回闭包调用的结果: ``` $result = value(true); // 真 $result = value(function () { return false; }); // 假 ``` #### `tap($value, $callback = null)` 函数接受两个参数: 任意`$value`和闭包。`$value`将被传递给闭包,并被`tap`函数返回。与闭包的返回值无关: ``` $user = tap(User::first(), function ($user) { $user->name = 'taylor'; $user->save(); }); ``` 如果没有向 tap 函数传递闭包,可以调用给定 $value 的任意方法。调用此方法的返回值永远是 $value,无论方法在其定义中返回什么。例如,Eloquent update 方法指定返回一个整数。但是,我们可以通过 tap 函数链式调用 update 方法强制其返回模型自身: ``` $user = tap($user)->update([ 'name' => $name, 'email' => $email, ]); ``` ***** #### `throw_unless()` 在给定的布尔表达式结果为`false`时,`throw_unless`函数抛出给定的异常: ``` throw_unless(Auth::user()->isAdmin(), AuthorizationException::class); throw_unless( Auth::user()->isAdmin(), AuthorizationException::class, 'You are not allowed to access this page' ); ``` ***** #### `throw_if()` 在给定的布尔表达式结果为`true`时,`throw_if`函数抛出给定的异常: ``` throw_if(! Auth::user()->isAdmin(), AuthorizationException::class); throw_if( ! Auth::user()->isAdmin(), AuthorizationException::class, 'You are not allowed to access this page' ); ``` ***** ## 字符串 要使用数组类,首先需要在你的类文件中引入 ~~~ use think\helper\Str; ~~~ ***** // 检查字符串中是否包含某些字符串 Str::contains($haystack, $needles) // 检查字符串是否以某些字符串结尾 Str::endsWith($haystack, $needles) // 获取指定长度的随机字母数字组合的字符串 Str::random($length = 16) // 字符串转小写 Str::lower($value) // 字符串转大写 Str::upper($value) // 获取字符串的长度 Str::length($value) // 截取字符串 Str::substr($string, $start, $length = null)