ZetCode

PHP 数组函数

最后修改于 2025 年 2 月 17 日

在本文中,我们将展示如何使用各种数组函数在 PHP 中使用数组。 数组是 PHP 中的一种基本数据结构,并且该语言提供了一组丰富的函数来操作它们。 我们将在示例中使用数字和字符串数据。

计数元素

以下示例演示了如何使用 count 函数计算数组中的元素数量。

main.php
<?php

$numbers = [1, 2, 3, 4, 5];
$fruits = [
    "apple" => "red",
    "banana" => "yellow",
    "grape" => "purple"
];

echo "Number of elements in numbers array: " . count($numbers) . "\n";
echo "Number of elements in fruits array: " . count($fruits) . "\n";

在本程序中,使用 count 函数计算 $numbers$fruits 数组中的元素数量。

$ php main.php
Number of elements in numbers array: 5
Number of elements in fruits array: 3

连接数组元素

以下示例演示了如何使用 implode 函数将数组的元素连接成一个字符串。

main.php
<?php

$fruits = ['apple', 'banana', 'grape', 'orange'];

$joinedFruits = implode(', ', $fruits);

echo "Joined fruits: " . $joinedFruits . "\n";

在本程序中,使用 implode 函数将 $fruits 数组的所有元素连接成一个字符串,用逗号分隔。

$ php main.php
Joined fruits: apple, banana, grape, orange

求和数组元素

以下示例演示了如何使用 array_sum 函数对数组中的所有元素求和。

main.php
<?php

$numbers = [1, 2, 3, 4, 5];

echo "Sum of elements in numbers array: " . array_sum($numbers) . "\n";

在本程序中,使用 array_sum 函数计算 $numbers 数组中所有元素的总和。

$ php main.php
Sum of elements in numbers array: 15

从数组中选取随机元素

以下示例演示了如何使用 array_rand 函数从数组中选取一个或多个随机元素。

main.php
<?php

$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$randomKey = array_rand($numbers);
$randomElement = $numbers[$randomKey];

echo "Random element: " . $randomElement . "\n";

// Picking multiple random elements
$randomKeys = array_rand($numbers, 3);
$randomElements = array_map(function ($key) use ($numbers) {
    return $numbers[$key];
}, $randomKeys);

echo "Multiple random elements: " . implode(', ', $randomElements) . "\n";

在本程序中,使用 array_rand 函数从 $numbers 数组中选取随机键。 然后访问这些键处的元素以获取随机值。

$ php main.php
Random element: 7
Multiple random elements: 2, 8, 10

排序数组

以下示例演示了如何使用 sortrsortasortksort 函数对数组进行排序。

main.php
<?php

$numbers = [3, 1, 4, 1, 5, 9];
$fruits = [
    "apple" => "red",
    "banana" => "yellow",
    "grape" => "purple"
];

// Sort numerically
sort($numbers);
print_r($numbers);

// Sort numerically in reverse order
rsort($numbers);
print_r($numbers);

// Sort associative array by value
asort($fruits);
print_r($fruits);

// Sort associative array by key
ksort($fruits);
print_r($fruits);

在本程序中,sort 函数按升序对 $numbers 数组进行排序,而 rsort 按降序排序。 asort 函数按值对 $fruits 数组进行排序,而 ksort 按键排序。

$ php main.php
Array
(
    [0] => 1
    [1] => 1
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 9
)
Array
(
    [0] => 9
    [1] => 5
    [2] => 4
    [3] => 3
    [4] => 1
    [5] => 1
)
Array
(
    [apple] => red
    [grape] => purple
    [banana] => yellow
)
Array
(
    [apple] => red
    [banana] => yellow
    [grape] => purple
)

从数组中提取列

以下示例演示了如何使用 array_column 函数从多维数组中提取一列。

main.php
<?php

$records = [
    ['name' => 'John', 'age' => 25],
    ['name' => 'Jane', 'age' => 22],
    ['name' => 'Doe', 'age' => 30]
];

$names = array_column($records, 'name');

print_r($names);

在本程序中,使用 array_column 函数从 $records 数组中提取 'name' 列的值。

$ php main.php
Array
(
    [0] => John
    [1] => Jane
    [2] => Doe
)

打乱数组

以下示例演示了如何使用 shuffle 函数打乱数组的元素。

main.php
<?php

$numbers = [1, 2, 3, 4, 5];

shuffle($numbers);

print_r($numbers);

在本程序中,使用 shuffle 函数随机重新排列 $numbers 数组中的元素。

$ php main.php
Array
(
    [0] => 3
    [1] => 1
    [2] => 4
    [3] => 5
    [4] => 2
)

合并数组

以下示例演示了如何使用 array_merge 函数合并数组。

main.php
<?php

$numbers1 = [1, 2, 3];
$numbers2 = [4, 5, 6];
$fruits1 = ["apple" => "red"];
$fruits2 = ["banana" => "yellow"];

// Merge numerical arrays
$mergedNumbers = array_merge($numbers1, $numbers2);
print_r($mergedNumbers);

// Merge associative arrays
$mergedFruits = array_merge($fruits1, $fruits2);
print_r($mergedFruits);

在本程序中,使用 array_merge 函数合并两个数值数组和两个关联数组。

$ php main.php
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Array
(
    [apple] => red
    [banana] => yellow
)

过滤数组

以下示例演示了如何使用 array_filter 函数过滤数组。

main.php
<?php

$numbers = [1, 2, 3, 4, 5, 6];
$fruits = [
    "apple" => "red",
    "banana" => "yellow",
    "grape" => "purple"
];

// Filter even numbers
$evenNumbers = array_filter($numbers, function($n) {
    return $n % 2 == 0;
});
print_r($evenNumbers);

// Filter fruits with names longer than 5 characters
$longFruits = array_filter($fruits, function($key) {
    return strlen($key) > 5;
}, ARRAY_FILTER_USE_KEY);
print_r($longFruits);

在本程序中,使用 array_filter 函数从 $numbers 数组中过滤偶数,并从 $fruits 数组中过滤名称超过 5 个字符的 fruit。

$ php main.php
Array
(
    [1] => 2
    [3] => 4
    [5] => 6
)
Array
(
    [banana] => yellow
)

映射数组

以下示例演示了如何使用 array_map 函数将一个函数应用于数组的所有元素。

main.php
<?php

$numbers = [1, 2, 3, 4, 5];
$fruits = ["apple", "banana", "grape"];

// Square all numbers
$squaredNumbers = array_map(function($n) {
    return $n * $n;
}, $numbers);

print_r($squaredNumbers);

// Uppercase all fruit names
$uppercaseFruits = array_map('strtoupper', $fruits);
print_r($uppercaseFruits);

在本程序中,使用 array_map 函数对 $numbers 数组中的所有数字进行平方,并将 $fruits 数组中的所有水果名称转换为大写。

$ php main.php
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)
Array
(
    [0] => APPLE
    [1] => BANANA
    [2] => GRAPE
)

在数组中查找元素

以下示例演示了如何使用自定义 array_find 函数在数组中查找元素。

main.php
<?php

function array_find($array, $predicate) {
    foreach ($array as $key => $value) {
        if ($predicate($value)) {
            return $value;
        }
    }
    return null;
}

$fruits = ['apple', 'banana', 'grape', 'orange'];

$result = array_find($fruits, function ($fruit) {
    return $fruit === 'banana';
});

echo "Found fruit: " . $result . "\n";

在本程序中,定义了一个自定义的 array_find 函数,用于在 $fruits 数组中查找与谓词函数指定的条件匹配的第一个元素。

$ php main.php
Found fruit: banana

来源

PHP 数组函数 - 文档

在本文中,我们展示了如何使用各种数组函数在 PHP 中使用数组。 数组是存储和操作数据集合的强大工具。

作者

我的名字是 Jan Bodnar,我是一位充满激情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。 迄今为止,我已撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出 所有 PHP 教程