数组助手
数组助手提供了一些函数来简化更复杂的数组使用。它不打算复制 PHP 提供的任何现有功能 - 除非是为了极大地简化它们的用法。
加载此助手
此助手使用以下代码加载
<?php
helper('array');
可用函数
以下函数可用
- dot_array_search(string $search, array $values)
- 参数:
$search (
string
) – 描述如何搜索数组的点符号字符串$values (
array
) – 要搜索的数组
- 返回值:
在数组中找到的值,或 null
- 返回值类型:
混合
此方法允许您使用点符号在数组中搜索特定键,并允许使用“*”通配符。给定以下数组
<?php $data = [ 'foo' => [ 'buzz' => [ 'fizz' => 11, ], 'bar' => [ 'baz' => 23, ], ], ];
我们可以使用搜索字符串“foo.buzz.fizz”找到“fizz”的值。同样,可以使用“foo.bar.baz”找到baz的值
<?php // Returns: 11 $fizz = dot_array_search('foo.buzz.fizz', $data); // Returns: 23 $baz = dot_array_search('foo.bar.baz', $data);
您可以使用星号作为通配符来替换任何段。找到后,它将搜索所有子节点,直到找到它。如果您不知道值,或者您的值具有数字索引,这将非常有用
<?php // Returns: 23 $baz = dot_array_search('foo.*.baz', $data);
如果数组键包含点,则可以使用反斜杠转义该键
<?php $data = [ 'foo' => [ 'bar.baz' => 23, ], 'foo.bar' => [ 'baz' => 43, ], ]; // Returns: 23 $barBaz = dot_array_search('foo.bar\.baz', $data); // Returns: 43 $fooBar = dot_array_search('foo\.bar.baz', $data);
注意
在 v4.2.0 之前,dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])
返回 23
,因为存在错误。v4.2.0 及更高版本返回 null
。
- array_deep_search($key, array $array)
- 参数:
$key (
mixed
) – 目标键$array (
array
) – 要搜索的数组
- 返回值:
在数组中找到的值,或 null
- 返回值类型:
混合
返回具有不确定深度的数组中具有键值的元素的值
- array_sort_by_multiple_keys(array &$array, array $sortColumns)
- 参数:
$array (
array
) – 要排序的数组(按引用传递)。$sortColumns (
array
) – 要排序的数组键以及相应的 PHP 排序标志,作为关联数组。
- 返回值:
排序是否成功。
- 返回值类型:
布尔值
此方法按层次结构对多维数组的元素进行排序,排序依据是数组中一个或多个键的值。例如,考虑以下可能由模型的
find()
函数返回的数组<?php $players = [ 0 => [ 'name' => 'John', 'team_id' => 2, 'position' => 3, 'team' => [ 'id' => 1, 'order' => 2, ], ], 1 => [ 'name' => 'Maria', 'team_id' => 5, 'position' => 4, 'team' => [ 'id' => 5, 'order' => 1, ], ], 2 => [ 'name' => 'Frank', 'team_id' => 5, 'position' => 1, 'team' => [ 'id' => 5, 'order' => 1, ], ], ];
现在,按两个键对该数组进行排序。请注意,该方法支持使用点符号访问更深层的数组级别中的值,但不支持通配符。
<?php array_sort_by_multiple_keys($players, [ 'team.order' => SORT_ASC, 'position' => SORT_ASC, ]);
现在,
$players
数组按每个玩家的 ‘team’ 子数组中的 ‘order’ 值进行排序。如果多个玩家的该值相同,则按玩家的 ‘position’ 值进行排序。排序后的数组如下所示:<?php $players = [ 0 => [ 'name' => 'Frank', 'team_id' => 5, 'position' => 1, 'team' => [ 'id' => 5, 'order' => 1, ], ], 1 => [ 'name' => 'Maria', 'team_id' => 5, 'position' => 4, 'team' => [ 'id' => 5, 'order' => 1, ], ], 2 => [ 'name' => 'John', 'team_id' => 2, 'position' => 3, 'team' => [ 'id' => 1, 'order' => 2, ], ], ];
同样,该方法也可以处理对象数组。在上面的示例中,每个 ‘player’ 可能由一个数组表示,而 ‘teams’ 是对象。该方法将检测每个嵌套级别的元素类型并相应地进行处理。
- array_flatten_with_dots(iterable $array[, string $id = '']) array
- 参数:
$array (
iterable
) – 要展平的多维数组$id (
string
) – 可选的 ID,用于在外部键之前添加。在内部用于展平键。
- 返回值类型:
array
- 返回值:
展平后的数组
此函数使用点作为分隔符,将多维数组展平为单个键值数组。
<?php $arrayToFlatten = [ 'personal' => [ 'first_name' => 'john', 'last_name' => 'smith', 'age' => '26', 'address' => 'US', ], 'other_details' => 'marines officer', ]; $flattened = array_flatten_with_dots($arrayToFlatten);
检查后,
$flattened
等于<?php [ 'personal.first_name' => 'john', 'personal.last_name' => 'smith', 'personal.age' => '26', 'personal.address' => 'US', 'other_details' => 'marines officer', ];
用户可以自行使用
$id
参数,但不是必需的。该函数在内部使用此参数来跟踪展平的键。如果用户提供初始$id
,它将被添加到所有键的前面。<?php // using the same data from above $flattened = array_flatten_with_dots($arrayToFlatten, 'foo_'); /* * $flattened is now: * [ * 'foo_personal.first_name' => 'john', * 'foo_personal.last_name' => 'smith', * 'foo_personal.age' => '26', * 'foo_personal.address' => 'US', * 'foo_other_details' => 'marines officer', * ] */
- array_group_by(array $array, array $indexes[, bool $includeEmpty = false]) array
- 参数:
$array (
array
) – 数据行(最有可能来自查询结果)$indexes (
array
) – 用于分组值的索引。遵循点语法$includeEmpty (
bool
) – 如果为真,则不筛选掉null
和''
值
- 返回值类型:
array
- 返回值:
一个按索引值分组的数组
此函数允许您按索引值将数据行分组在一起。返回数组的深度等于作为参数传递的索引数量。
示例显示了一些数据(例如,从 API 加载),其中包含嵌套数组。
<?php $employees = [ [ 'id' => 1, 'first_name' => 'Urbano', 'gender' => null, 'hr' => [ 'country' => 'Canada', 'department' => 'Engineering', ], ], [ 'id' => 2, 'first_name' => 'Case', 'gender' => 'Male', 'hr' => [ 'country' => null, 'department' => 'Marketing', ], ], [ 'id' => 3, 'first_name' => 'Emera', 'gender' => 'Female', 'hr' => [ 'country' => 'France', 'department' => 'Engineering', ], ], [ 'id' => 4, 'first_name' => 'Richy', 'gender' => null, 'hr' => [ 'country' => null, 'department' => 'Sales', ], ], [ 'id' => 5, 'first_name' => 'Mandy', 'gender' => null, 'hr' => [ 'country' => 'France', 'department' => 'Sales', ], ], [ 'id' => 6, 'first_name' => 'Risa', 'gender' => 'Female', 'hr' => [ 'country' => null, 'department' => 'Engineering', ], ], [ 'id' => 7, 'first_name' => 'Alfred', 'gender' => 'Male', 'hr' => [ 'country' => 'France', 'department' => 'Engineering', ], ], [ 'id' => 8, 'first_name' => 'Tabby', 'gender' => 'Male', 'hr' => [ 'country' => 'France', 'department' => 'Marketing', ], ], [ 'id' => 9, 'first_name' => 'Ario', 'gender' => 'Male', 'hr' => [ 'country' => null, 'department' => 'Sales', ], ], [ 'id' => 10, 'first_name' => 'Somerset', 'gender' => 'Male', 'hr' => [ 'country' => 'Germany', 'department' => 'Marketing', ], ], ];
我们希望首先按“gender”分组,然后按“hr.department”分组(最大深度 = 2)。首先是排除空值的结果
<?php $result = array_group_by($employees, ['gender', 'hr.department']); $result = [ 'Male' => [ 'Marketing' => [ [ 'id' => 2, 'first_name' => 'Case', 'gender' => 'Male', 'hr' => [ 'country' => null, 'department' => 'Marketing', ], ], [ 'id' => 8, 'first_name' => 'Tabby', 'gender' => 'Male', 'hr' => [ 'country' => 'France', 'department' => 'Marketing', ], ], [ 'id' => 10, 'first_name' => 'Somerset', 'gender' => 'Male', 'hr' => [ 'country' => 'Germany', 'department' => 'Marketing', ], ], ], 'Engineering' => [ [ 'id' => 7, 'first_name' => 'Alfred', 'gender' => 'Male', 'hr' => [ 'country' => 'France', 'department' => 'Engineering', ], ], ], 'Sales' => [ [ 'id' => 9, 'first_name' => 'Ario', 'gender' => 'Male', 'hr' => [ 'country' => null, 'department' => 'Sales', ], ], ], ], 'Female' => [ 'Engineering' => [ [ 'id' => 3, 'first_name' => 'Emera', 'gender' => 'Female', 'hr' => [ 'country' => 'France', 'department' => 'Engineering', ], ], [ 'id' => 6, 'first_name' => 'Risa', 'gender' => 'Female', 'hr' => [ 'country' => null, 'department' => 'Engineering', ], ], ], ], ];
这里使用相同的代码,但这次我们希望包含空值
<?php $result = array_group_by($employees, ['gender', 'hr.department'], true); $result = [ '' => [ 'Engineering' => [ [ 'id' => 1, 'first_name' => 'Urbano', 'gender' => null, 'hr' => [ 'country' => 'Canada', 'department' => 'Engineering', ], ], ], 'Sales' => [ [ 'id' => 4, 'first_name' => 'Richy', 'gender' => null, 'hr' => [ 'country' => null, 'department' => 'Sales', ], ], [ 'id' => 5, 'first_name' => 'Mandy', 'gender' => null, 'hr' => [ 'country' => 'France', 'department' => 'Sales', ], ], ], ], 'Male' => [ 'Marketing' => [ [ 'id' => 2, 'first_name' => 'Case', 'gender' => 'Male', 'hr' => [ 'country' => null, 'department' => 'Marketing', ], ], [ 'id' => 8, 'first_name' => 'Tabby', 'gender' => 'Male', 'hr' => [ 'country' => 'France', 'department' => 'Marketing', ], ], [ 'id' => 10, 'first_name' => 'Somerset', 'gender' => 'Male', 'hr' => [ 'country' => 'Germany', 'department' => 'Marketing', ], ], ], 'Engineering' => [ [ 'id' => 7, 'first_name' => 'Alfred', 'gender' => 'Male', 'hr' => [ 'country' => 'France', 'department' => 'Engineering', ], ], ], 'Sales' => [ [ 'id' => 9, 'first_name' => 'Ario', 'gender' => 'Male', 'hr' => [ 'country' => null, 'department' => 'Sales', ], ], ], ], 'Female' => [ 'Engineering' => [ [ 'id' => 3, 'first_name' => 'Emera', 'gender' => 'Female', 'hr' => [ 'country' => 'France', 'department' => 'Engineering', ], ], [ 'id' => 6, 'first_name' => 'Risa', 'gender' => 'Female', 'hr' => [ 'country' => null, 'department' => 'Engineering', ], ], ], ], ];