多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 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 数组函数。