数组是 PHP 中最常用的数据结构,既可以作为列表,也可以作为键值映射。字符串处理函数也非常丰富。

本节要点

  • 索引数组使用数字下标,关联数组使用字符串键。
  • array_map、array_filter、array_reduce 适合处理数组。
  • explode、implode、trim、str_replace 是常用字符串函数。

代码示例

<?php
$users = [
    ["id" => 1, "name" => "小明"],
    ["id" => 2, "name" => "小红"],
];

$names = array_map(fn($u) => $u["name"], $users);
echo implode(", ", $names);

$tags = "html,css,js";
$list = explode(",", $tags);
print_r($list);