<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國際加速解決方案。 廣告
                # PHP 數組函數 > 原文: [https://zetcode.com/lang/php/arrayfunctions/](https://zetcode.com/lang/php/arrayfunctions/) 在上一章中,我們討論了數組的初始化和細讀。 在本章中,我們將介紹各種 PHP 數組函數。 這些功能使我們能夠修改,排序,合并,切片和隨機排列數組內的數據。 ## PHP 排序數組 首先,我們將對數組進行排序。 `sort.php` ```php <?php $names = [ "Jane", "Rebecca", "Lucy", "Lenka", "Ada" ]; echo "Unsorted: \n"; foreach ($names as $name) { echo "$name "; } echo "\n"; sort($names); echo "Sorted: \n"; foreach ($names as $name) { echo "$name "; } echo "\n"; ``` 在上面的腳本中,我們有一個`$names`數組。 我們使用`sort()`函數對數組的內容進行排序。 ```php $ php sort.php Unsorted: Jane Rebecca Lucy Lenka Ada Sorted: Ada Jane Lenka Lucy Rebecca ``` 腳本的輸出顯示未排序和已排序的女性姓名。 `rsort()`函數以相反的順序對數組進行排序。 `sort2.php` ```php <?php $numbers = [ 12, 3, 5, 1, 6, 7, 10, 0, 9, 8, 11]; sort($numbers); echo "Ascending order: \n"; foreach ($numbers as $n) { echo "$n "; } echo "\n"; rsort($numbers); echo "Descending order: \n"; foreach ($numbers as $n) { echo "$n "; } echo "\n"; ``` 有一個整數數組。 它按升序和降序排序。 ```php sort($numbers); ``` `sort()`函數按升序對整數進行排序。 ```php rsort($numbers); ``` `rsort()`函數按降序對整數進行排序。 ```php $ php sort2.php Ascending order: 0 1 3 5 6 7 8 9 10 11 12 Descending order: 12 11 10 9 8 7 6 5 3 1 0 ``` 這是`sort2.php`腳本的輸出。 在下面的示例中,我們顯示了如何對重音字符進行排序。 `locale_sort.php` ```php <?php setlocale(LC_ALL, 'sk_SK.utf8'); $words = [ "?ate?", "auto", "?elezo", "byt", "kocka", "dáma", "zem", "autor", "cedu?a", "?i?ma"]; sort($words, SORT_LOCALE_STRING); echo "Ascending order: \n"; foreach ($words as $w) { echo "$w "; } echo "\n"; rsort($words, SORT_LOCALE_STRING); echo "Descending order: \n"; foreach ($words as $w) { echo "$w "; } echo "\n"; ``` 我們有一系列包含特定口音的斯洛伐克語單詞。 ```php setlocale(LC_ALL, 'sk_SK.utf8'); ``` 我們使用`setlocale()`函數設置斯洛伐克語區域設置。 語言環境代表特定的地理,政治或文化區域。 ```php $words = [ "?ate?", "auto", "?elezo", "byt", "kocka", "dáma", "zem", "autor", "cedu?a", "?i?ma"]; ``` `$words`是帶有重音符號的斯洛伐克語單詞的數組。 ```php sort($words, SORT_LOCALE_STRING); ``` 我們使用`sort()`函數以升序對數組進行排序。 我們向函數傳遞`SORT_LOCALE_STRING`標志,該標志告訴`sort()`考慮到語言環境。 ```php $ php locale_sort.php Ascending order: auto autor byt cedu?a ?i?ma dáma ?ate? kocka zem ?elezo Descending order: ?elezo zem kocka ?ate? dáma ?i?ma cedu?a byt autor auto ``` 單詞已根據斯洛伐克語標準正確排序。 有時我們需要執行自定義排序。 對于自定義排序,我們在 PHP 中具有`usort()`函數。 `custom_sorting.php` ```php <?php $names = [ "Michael Brown", "Albert Einstein", "Gerry Miller", "Tom Willis", "Michael Gray", "Luke Smith" ]; function sort_second_names($a, $b) { $name1 = explode(" ", $a); $name2 = explode(" ", $b); return strcmp($name1[1], $name2[1]); } usort($names, 'sort_second_names'); foreach ($names as $name) { echo "$name\n"; } echo "\n"; ``` 我們有一個全名數組。 `sort()`函數將根據名字對這些字符串進行排序,因為它們在第二個名字之前。 我們創建了一個解決方案,以根據它們的名字對這些名字進行排序。 ```php function sort_second_names($a, $b) { $name1 = explode(" ", $a); $name2 = explode(" ", $b); return strcmp($name1[1], $name2[1]); } ``` 我們有一個自定義的排序函數。 名稱由`explode()`函數分割,并將第二個名稱與`strcmp()`函數進行比較。 ```php usort($names, 'sort_second_names'); ``` `usort()`函數接受比較函數作為其第二個參數。 ```php $ php custom_sorting.php Michael Brown Albert Einstein Michael Gray Gerry Miller Luke Smith Tom Willis ``` 名稱將根據其第二名正確排序。 ## PHP 計算數組大小 `count()`函數對數組中的元素數進行計數。 `array_sum()`函數計算所有值的總和。 `array_product()`函數計算數組中值的乘積。 `counting.php` ```php <?php $numbers = [ 1, 2, 4, 5, 2, 3, 5, 2 ]; $len = count($numbers); $sum = array_sum($numbers); $prod = array_product($numbers); echo "In the array, there are $len numbers\n"; echo "The sum of the numbers is $sum\n"; echo "The product of the numbers is $prod\n"; ``` 在示例中,我們有一個數字數組。 我們將上述定義的函數應用于數組。 ```php $ php counting.php In the array, there are 8 numbers The sum of the numbers is 24 The product of the numbers is 2400 ``` 這是腳本的輸出。 ## PHP 唯一值 在下面的示例中,我們在數組中找出唯一值。 `unique.php` ```php <?php $numbers = array(3, 4, 4, 3, 2, 4); $count_values = array_count_values($numbers); print_r($count_values); $unique = array_unique($numbers); print_r($unique); ``` 在此腳本中,數組中有重復項。 `array_count_values()`函數返回一個數組,其中包含每個值的出現次數。 `array_unique()`函數返回一個沒有重復的數組。 ```php $ php unique.php Array ( [3] => 2 [4] => 3 [2] => 1 ) Array ( [0] => 3 [1] => 4 [4] => 2 ) ``` 第一個數組表示 3 次出現兩次,4 次出現 3 次,2 次出現一次。 第二個數組表示數組中存在三個值:3、4 和 2。值 3 的索引為 0,4 的索引為 1,2 的索引為 4。`array_unique()`函數保持索引不變。 ## PHP 切片數組 `array_slice()`函數從數組中返回其偏移量和長度參數所指定的元素序列。 `slicing.php` ```php <?php $nums = range(1, 20); $slice1 = array_slice($nums, 0, 3); echo "Slice1:\n"; foreach ($slice1 as $s) { echo "$s "; } echo "\n"; $slice2 = array_slice($nums, -3); echo "Slice2:\n"; foreach ($slice2 as $s) { echo "$s "; } echo "\n"; ``` 在示例中,我們創建了一個整數數組的兩個切片。 ```php $slice1 = array_slice($nums, 0, 3); ``` 我們從第一個元素開始創建一個切片; 切片的長度是三個元素。 ```php $slice2 = array_slice($nums, -3); ``` 通過提供負偏移量,可以從數組的末尾創建切片。 ```php $ php slicing.php Slice1: 1 2 3 Slice2: 18 19 20 ``` 這是`slicing.php`示例的輸出。 ## PHP 數組指針 PHP 具有內部數組指針。 在下面的示例中,我們介紹了操作該指針的函數。 `array_pointer.php` ```php <?php $continents = [ "America", "Africa", "Europe", "Asia", "Australia", "Antarctica" ]; $item1 = current($continents); $item2 = next($continents); $item3 = next($continents); $item4 = end($continents); $item5 = prev($continents); echo "$item1, $item2, $item3, $item4, $item5\n"; reset($continents); while(list($idx, $val) = each($continents)) { echo "Index: $idx, Value: $val\n"; } ``` 在此示例中,我們使用移動內部數組指針的函數遍歷數組。 ```php $item1 = current($continents); $item2 = next($continents); $item3 = next($continents); $item4 = end($continents); $item5 = prev($continents); ``` `current()`函數返回數組中的當前元素。 首先,它是數組的第一個元素。 `next()`函數使指針前進一個位置。 `end()`函數返回最后一個元素。 `prev()`元素返回該元素,即當前元素之前的一個位置。 在我們的情況下,它是最后一個元素的下一個。 ```php reset($continents); while(list($idx, $val) = each($continents)) { echo "Index: $idx, Value: $val\n"; } ``` 在這里,我們使用`reset()`函數再次將內部指針設置為第一個元素,并仔細閱讀`$continents`數組。 ```php $ php array_pointer.php America, Africa, Europe, Antarctica, Australia Index: 0, Value: America Index: 1, Value: Africa Index: 2, Value: Europe Index: 3, Value: Asia Index: 4, Value: Australia Index: 5, Value: Antarctica ``` 這是`array_pointer.php`腳本的輸出。 ## PHP 合并數組 `array_merge()`函數合并數組。 `merge.php` ```php <?php $names1 = [ "Jane", "Lucy", "Rebecca" ]; $names2 = [ "Lenka", "Timea", "Victoria" ]; $names = array_merge($names1, $names2); foreach ($names as $name) { echo "$name "; } echo "\n"; ``` 在此示例中,我們有兩個數組:`$names1`和`$names2`。 我們使用`array_merge()`函數通過合并前兩個數組來創建`$names`數組。 ```php $ php merge.php Jane Lucy Rebecca Lenka Timea Victoria ``` 新數組有六個名稱。 ## PHP 修改數組 可以使用`array_push()`,`array_pop()`,`array_shift()`或`array_unshift()`函數修改 PHP 數組。 `modify.php` ```php <?php $numbers = [ 1, 2, 3, 4 ]; array_push($numbers, 5, 6); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; array_pop($numbers); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; array_unshift($numbers, -1, 0); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; array_shift($numbers); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; ``` 在上面的腳本中,我們使用了修改數組內容的函數。 我們有一個`$numbers`數組,其中包含 4 個數字:1、2、3 和 4。 ```php array_push($numbers, 5, 6); ``` `array_push()`函數將一個或多個項目插入數組的末尾。 現在,我們的數組包含值 1、2、3、4、5 和 6。 ```php array_pop($numbers); ``` `array_pop()`函數從數組中刪除最后一項。 我們的數組現在存儲數字 1、2、3、4 和 5。 ```php array_unshift($numbers, -1, 0); ``` `array_unshift()`函數將 -1 和 0 添加到數組的開頭。 該數組包含值-1、0、1、2、3、4 和 5。 ```php array_shift($numbers); ``` 最后,`array_shift()`函數從數組中刪除第一項。 現在我們在數組中具有值 0、1、2、3、4 和 5。 ```php $ php modify.php 1 2 3 4 5 6 1 2 3 4 5 -1 0 1 2 3 4 5 0 1 2 3 4 5 ``` 這是`modify.php`示例的輸出。 ## PHP 范圍函數 `range()`函數通過自動創建元素序列來簡化數組創建。 它接受三個參數:序列開始,序列結束和可選的增量,默認為 1。 `range.php` ```php <?php $numbers1 = range(1, 15); foreach ($numbers1 as $num) { echo "$num "; } echo "\n"; $numbers2 = range(15, 1, -1); foreach ($numbers2 as $num) { echo "$num "; } echo "\n"; ``` `range()`函數使我們能夠輕松創建連續數字列表。 ```php $numbers1 = range(1, 15); ``` 創建一個數字為 1,2,... 15 的數組。 ```php $numbers2 = range(15, 1, -1); ``` 通過指定負增量可以創建值的降序序列。 ```php $ php range.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ``` 這是`range.php`函數的輸出。 ## PHP 隨機化數組值 `array_rand()`函數從數組中選擇一個或多個隨機條目。 `shuffle()`函數可將數組中元素的順序隨機化。 `randomize.php` ```php <?php $nums = range(1, 20); echo ($nums[array_rand($nums)]) . "\n"; $r = array_rand($nums, 2); echo $nums[$r[0]] . "\n"; echo $nums[$r[1]] . "\n"; shuffle($nums); foreach ($nums as $n) { echo "$n "; } echo "\n"; ``` 在示例中,我們從數組中選擇隨機值,并隨機化其元素順序。 ```php echo ($nums[array_rand($nums)]) . "\n"; ``` `array_rand()`函數從`$num`數組返回一個隨機鍵。 ```php $r = array_rand($nums, 2); ``` 在這種情況下,`array_rand()`函數返回兩個隨機鍵的數組。 ```php $ php randomize.php 4 2 19 13 19 4 3 17 11 20 16 10 9 8 14 15 12 18 2 6 5 1 7 ``` 這是`randomize.php`程序的示例輸出。 ## PHP `in_array`函數 `in_array()`函數檢查數組中是否有特定元素。 `inarray.php` ```php <?php $names = [ "Jane", "Adriana", "Lucy", "Rebecca" ]; if (in_array("Jane", $names)) { echo "Jane is in the array\n"; } else { echo "Jane is not in the array\n"; } if (in_array("Monica", $names)) { echo "Monica is in the array\n"; } else { echo "Monica is not in the array\n"; } ``` 我們的腳本檢查`$names`數組中是否為'Jane'和'Monica'。 ```php $ php inarray.php Jane is in the array Monica is not in the array ``` 簡中有簡,但莫妮卡中沒有。 ## PHP 數組鍵和值 PHP 數組是一個由鍵和值對組成的關聯數組。 `keysvalues.php` ```php <?php $domains = [ "sk" => "Slovakia", "de" => "Germany", "hu" => "Hungary", "ru" => "Russia" ]; $keys = array_keys($domains); $values = array_values($domains); foreach ($keys as $key) { echo "$key "; } echo "\n"; foreach ($values as $value) { echo "$value "; } echo "\n"; ``` `array_keys()`函數返回數組的所有鍵。 `array_values()`函數返回數組的所有值。 ```php $ php keysvalues.php sk de hu ru Slovakia Germany Hungary Russia ``` 第一行包含頂級域名。 這些是`$domains`數組的鍵。 第二行是相應國家的名稱。 這些是數組的值。 ## PHP `array_walk`函數 `array_walk()`函數將用戶定義的函數應用于數組的每個成員。 `array_walk.php` ```php <?php $countries = [ "de" => "Germany", "sk" => "Slovakia", "us" => "United States", "ru" => "Russia", "hu" => "Hungaria", "pl" => "Poland" ]; function show_values($value, $key) { echo "The $key stands for the $value\n"; } array_walk($countries, 'show_values'); ``` 我們有一個`$countries`數組。 我們將`show_values`函數應用于數組的每個元素。 該函數僅打印每個元素的鍵和值。 ```php $ php array_walk.php The de stands for the Germany The sk stands for the Slovakia The us stands for the United States The ru stands for the Russia The hu stands for the Hungaria The pl stands for the Poland ``` 這是`array_walk()`函數的輸出。 在 PHP 教程的這一部分中,我們介紹了一些 PHP 數組函數。
                  <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>

                              哎呀哎呀视频在线观看