<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看