多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 数组助手函数 ### [](https://octobercms.com/docs/services/helpers#arrays)数组 #### [](https://octobercms.com/docs/services/helpers#method-array-add)`array_add()` 如果`array_add`给定键/值对在数组中不存在,则该函数将其添加到数组: ~~~ $array = array_add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-divide)`array_divide()` 该`array_divide`函数返回两个数组,一个包含键,另一个包含原始数组的值: ~~~ list($keys, $values) = array_divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk'] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-dot)`array_dot()` 该`array_dot`函数将多维数组展平为使用“点”符号表示深度的单级数组: ~~~ $array = array_dot(['foo' => ['bar' => 'baz']]); // ['foo.bar' => 'baz']; ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-undot)`array_undot()` 该`array_undot`功能是该`array_dot`方法的对立部分。它将点标记的数组转换为标准的关联数组: ~~~ $array = array_undot([ 'foo.bar' => 'baz' ]); // [ // 'foo' => [ // 'bar' => 'baz' // ] // ] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-except)`array_except()` 该`array_except`方法从数组中删除给定的键/值对: ~~~ $array = ['name' => 'Desk', 'price' => 100]; $array = array_except($array, ['price']); // ['name' => 'Desk'] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-first)`array_first()` 该`array_first`方法返回通过给定真值测试的数组的第一个元素: ~~~ $array = [100, 200, 300]; $value = array_first($array, function ($key, $value) { return $value >= 150; }); // 200 ~~~ 还可以将默认值作为第三个参数传递给该方法。如果没有任何值通过真实性测试,则将返回此值: ~~~ $value = array_first($array, $callback, $default); ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-flatten)`array_flatten()` 该`array_flatten`方法将把多维数组展平为单个级别。 ~~~ $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $array = array_flatten($array); // ['Joe', 'PHP', 'Ruby']; ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-forget)`array_forget()` 该`array_forget`方法使用“点”符号从深度嵌套的数组中删除给定的键/值对: ~~~ $array = ['products' => ['desk' => ['price' => 100]]]; array_forget($array, 'products.desk'); // ['products' => []] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-get)`array_get()` 该`array_get`方法使用“点”表示法从深度嵌套的数组中检索值: ~~~ $array = ['products' => ['desk' => ['price' => 100]]]; $value = array_get($array, 'products.desk'); // ['price' => 100] ~~~ 该`array_get`函数还接受默认值,如果未找到特定键,则将返回该默认值: ~~~ $value = array_get($array, 'names.john', 'default'); ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-only)`array_only()` 该`array_only`方法将只返回给定数组中的指定键/值对: ~~~ $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $array = array_only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-pluck)`array_pluck()` 该`array_pluck`方法将从数组中提取给定键/值对的列表: ~~~ $array = [ ['developer' => ['name' => 'Brian']], ['developer' => ['name' => 'Stewie']] ]; $array = array_pluck($array, 'developer.name'); // ['Brian', 'Stewie']; ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-pull)`array_pull()` 该`array_pull`方法返回并从数组中删除键/值对: ~~~ $array = ['name' => 'Desk', 'price' => 100]; $name = array_pull($array, 'name'); // $name: Desk // $array: ['price' => 100] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-set)`array_set()` 该`array_set`方法使用“点”符号在深层嵌套的数组中设置一个值: ~~~ $array = ['products' => ['desk' => ['price' => 100]]]; array_set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-sort)`array_sort()` 该`array_sort`方法根据给定Closure的结果对数组进行排序: ~~~ $array = [ ['name' => 'Desk'], ['name' => 'Chair'], ]; $array = array_values(array_sort($array, function ($value) { return $value['name']; })); /* [ ['name' => 'Chair'], ['name' => 'Desk'], ] */ ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-sort-recursive)`array_sort_recursive()` 该`array_sort_recursive`函数使用以下`sort`函数对数组进行递归排序: ~~~ $array = [ [ 'Brian', 'Shannon', 'Alec', ], [ 'PHP', 'Ruby', 'JavaScript', ], ]; $array = array_sort_recursive($array); /* [ [ 'Alec', 'Brian', 'Shannon', ], [ 'JavaScript', 'PHP', 'Ruby', ] ]; */ ~~~ #### [](https://octobercms.com/docs/services/helpers#method-array-where)`array_where()` 该`array_where`函数使用给定的Closure过滤数组: ~~~ $array = [100, '200', 300, '400', 500]; $array = array_where($array, function ($value, $key) { return is_string($value); }); // [1 => 200, 3 => 400] ~~~ #### [](https://octobercms.com/docs/services/helpers#method-head)`head()` 该`head`函数仅返回给定数组中的第一个元素: ~~~ $array = [100, 200, 300]; $first = head($array); // 100 ~~~ #### [](https://octobercms.com/docs/services/helpers#method-last)`last()` 该`last`函数返回给定数组中的最后一个元素: ~~~ $array = [100, 200, 300]; $last = last($array); // 300 ~~~