PHP 数组

Indexed and associative arrays, CRUD operations, and common array functions.

Beginner
Key PointsA PHP array is an ordered key-value map — it doubles as list, dictionary and stack.

两种数组

PHP has one array type: an ordered hash table. Integer keys make it indexed, string keys make it associative — and you can mix them.

PHP
<?php
$fruits = ["apple", "banana", "cherry"];
echo count($fruits), "\n";
echo $fruits[1], "\n";

$user = ["name" => "Tom", "age" => 18];
echo $user["name"], "\n";

// 混合键
$mix = [0 => "a", "k" => "b", 5 => "c"];
print_r($mix);
Output
3
banana
Tom
Array
(
    [0] => a
    [k] => b
    [5] => c
)

增删改

PHP
<?php
$fruits = ["apple", "banana", "cherry"];

$user = ["name" => "Tom", "age" => 18];
$user["city"] = "上海";
print_r($user);

array_push($fruits, "date");
array_unshift($fruits, "zero");
print_r($fruits);

unset($fruits[0]);
print_r($fruits);
Output
Array
(
    [name] => Tom
    [age] => 18
    [city] => 上海
)
Array
(
    [0] => zero
    [1] => apple
    [2] => banana
    [3] => cherry
    [4] => date
)
Array
(
    [1] => apple
    [2] => banana
    [3] => cherry
    [4] => date
)
unset() removes the element but does not reindex. After deleting key 0 the keys start at 1, breaking index-based for loops. Use array_values() to reindex.
PHP
<?php
$arr = [1, 2, 3];
unset($arr[1]);

print_r($arr);
print_r(array_values($arr));
echo implode(",", $arr), "\n";
Output
Array
(
    [0] => 1
    [2] => 3
)
Array
(
    [0] => 1
    [1] => 3
)
1,3

常用函数

FunctionPurposeExample
count元素个数count([1,2,3]) → 3
array_keys所有键array_keys(["a"=>1]) → ["a"]
array_values所有值并重排键array_values([5=>1]) → [0=>1]
array_merge合并(字符串键覆盖)array_merge([1],[2]) → [1,2]
array_slice截取一段array_slice([1,2,3],1) → [2,3]
array_splice删除并替换会改动原数组
array_search查值返回键找不到返回 false
in_array判断值是否存在第三个参数 true 严格比
array_sum求和array_sum([1,2,3]) → 6
array_unique去重(保留首个键)键会留下空洞
array_filter按回调过滤保留原键
array_map逐个映射重新索引
array_column取二维数组某列报表场景常用
PHP
<?php
print_r(array_merge([1, 2], [3, 4]));

[$a, $b] = [10, 20];
echo $a + $b, "\n";

$spread = [...[1, 2], ...[3, 4]];
echo implode(",", $spread), "\n";

$rows = [
    ["id" => 1, "name" => "Tom"],
    ["id" => 2, "name" => "Amy"],
];
print_r(array_column($rows, "name"));
echo array_sum([1, 2, 3, 4]), "\n";
Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
30
1,2,3,4
Array
(
    [0] => Tom
    [1] => Amy
)
10

解构与展开

list() / [] destructuring works positionally or by key, including nested arrays.

PHP
<?php
[$a, , $c] = [1, 2, 3];        // 跳过第二个
echo "$a $c\n";

["name" => $n, "age" => $age] = ["name" => "Tom", "age" => 18];
echo "$n $age\n";

[[$x, $y], [$p, $q]] = [[1, 2], [3, 4]];
echo $x + $y + $p + $q, "\n";

function pair(): array {
    return [1, 2];
}
[$first, $second] = pair();
echo $first + $second, "\n";
Output
1 3
Tom 18
10
3

什么时候用数组、什么时候用对象

  • Temporary collections, config, multiple return values —— arrays suffice
  • Data with behaviour —— use a class, not a bag of agreed keys
  • External input —— arrays map naturally to JSON

Friends & Partners