资讯专栏INFORMATION COLUMN

30秒的PHP代码片段(1)数组 - Array

dunizb / 1033人阅读

摘要:排列如果所提供的函数返回的数量等于数组中成员数量的总和,则函数返回,否则返回。平铺数组将数组降为一维数组根据给定的函数对数组的元素进行分组。使用给定的回调筛选数组。相关文章秒的代码片段数学秒的代码片段字符串函数

本文来自GitHub开源项目

点我跳转

30秒的PHP代码片段

</>复制代码

  1. 精选的有用PHP片段集合,您可以在30秒或更短的时间内理解这些片段。
排列 all

如果所提供的函数返回 true 的数量等于数组中成员数量的总和,则函数返回 true,否则返回 false

</>复制代码

  1. function all($items, $func)
  2. {
  3. return count(array_filter($items, $func)) === count($items);
  4. }

Examples

</>复制代码

  1. all([2, 3, 4, 5], function ($item) {
  2. return $item > 1;
  3. }); // true
any

如果提供的函数对数组中的至少一个元素返回true,则返回true,否则返回false

</>复制代码

  1. function any($items, $func)
  2. {
  3. return count(array_filter($items, $func)) > 0;
  4. }

Examples

</>复制代码

  1. any([1, 2, 3, 4], function ($item) {
  2. return $item < 2;
  3. }); // true
deepFlatten(深度平铺数组)

将多维数组转为一维数组

</>复制代码

  1. function deepFlatten($items)
  2. {
  3. $result = [];
  4. foreach ($items as $item) {
  5. if (!is_array($item)) {
  6. $result[] = $item;
  7. } else {
  8. $result = array_merge($result, deepFlatten($item));
  9. }
  10. }
  11. return $result;
  12. }

Examples

</>复制代码

  1. deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]
drop

返回一个新数组,并从左侧弹出n个元素。

</>复制代码

  1. function drop($items, $n = 1)
  2. {
  3. return array_slice($items, $n);
  4. }

Examples

</>复制代码

  1. drop([1, 2, 3]); // [2,3]
  2. drop([1, 2, 3], 2); // [3]
findLast

返回所提供的函数为其返回的有效值(即过滤后的值)的最后一个元素的键值(value)。

</>复制代码

  1. function findLast($items, $func)
  2. {
  3. $filteredItems = array_filter($items, $func);
  4. return array_pop($filteredItems);
  5. }

Examples

</>复制代码

  1. findLast([1, 2, 3, 4], function ($n) {
  2. return ($n % 2) === 1;
  3. });
  4. // 3
findLastIndex

返回所提供的函数为其返回的有效值(即过滤后的值)的最后一个元素的键名(key)。

</>复制代码

  1. function findLastIndex($items, $func)
  2. {
  3. $keys = array_keys(array_filter($items, $func));
  4. return array_pop($keys);
  5. }

Examples

</>复制代码

  1. findLastIndex([1, 2, 3, 4], function ($n) {
  2. return ($n % 2) === 1;
  3. });
  4. // 2
flatten(平铺数组)

将数组降为一维数组

</>复制代码

  1. function flatten($items)
  2. {
  3. $result = [];
  4. foreach ($items as $item) {
  5. if (!is_array($item)) {
  6. $result[] = $item;
  7. } else {
  8. $result = array_merge($result, array_values($item));
  9. }
  10. }
  11. return $result;
  12. }

Examples

</>复制代码

  1. flatten([1, [2], 3, 4]); // [1, 2, 3, 4]
groupBy

根据给定的函数对数组的元素进行分组。

</>复制代码

  1. function groupBy($items, $func)
  2. {
  3. $group = [];
  4. foreach ($items as $item) {
  5. if ((!is_string($func) && is_callable($func)) || function_exists($func)) {
  6. $key = call_user_func($func, $item);
  7. $group[$key][] = $item;
  8. } elseif (is_object($item)) {
  9. $group[$item->{$func}][] = $item;
  10. } elseif (isset($item[$func])) {
  11. $group[$item[$func]][] = $item;
  12. }
  13. }
  14. return $group;
  15. }

Examples

</>复制代码

  1. groupBy(["one", "two", "three"], "strlen"); // [3 => ["one", "two"], 5 => ["three"]]
hasDuplicates(查重)

检查数组中的重复值。如果存在重复值,则返回true;如果所有值都是唯一的,则返回false

</>复制代码

  1. function hasDuplicates($items)
  2. {
  3. return count($items) > count(array_unique($items));
  4. }

Examples

</>复制代码

  1. hasDuplicates([1, 2, 3, 4, 5, 5]); // true
head

返回数组中的第一个元素。

</>复制代码

  1. function head($items)
  2. {
  3. return reset($items);
  4. }

Examples

</>复制代码

  1. head([1, 2, 3]); // 1
last

返回数组中的最后一个元素。

</>复制代码

  1. function last($items)
  2. {
  3. return end($items);
  4. }

Examples

</>复制代码

  1. last([1, 2, 3]); // 3
pluck

检索给定键名的所有键值

</>复制代码

  1. function pluck($items, $key)
  2. {
  3. return array_map( function($item) use ($key) {
  4. return is_object($item) ? $item->$key : $item[$key];
  5. }, $items);
  6. }

Examples

</>复制代码

  1. pluck([
  2. ["product_id" => "prod-100", "name" => "Desk"],
  3. ["product_id" => "prod-200", "name" => "Chair"],
  4. ], "name");
  5. // ["Desk", "Chair"]
pull

修改原始数组以过滤掉指定的值。

</>复制代码

  1. function pull(&$items, ...$params)
  2. {
  3. $items = array_values(array_diff($items, $params));
  4. return $items;
  5. }

Examples

</>复制代码

  1. $items = ["a", "b", "c", "a", "b", "c"];
  2. pull($items, "a", "c"); // $items will be ["b", "b"]
reject

使用给定的回调筛选数组。

</>复制代码

  1. function reject($items, $func)
  2. {
  3. return array_values(array_diff($items, array_filter($items, $func)));
  4. }

Examples

</>复制代码

  1. reject(["Apple", "Pear", "Kiwi", "Banana"], function ($item) {
  2. return strlen($item) > 4;
  3. }); // ["Pear", "Kiwi"]
remove

从给定函数返回false的数组中删除元素。

</>复制代码

  1. function remove($items, $func)
  2. {
  3. $filtered = array_filter($items, $func);
  4. return array_diff_key($items, $filtered);
  5. }

Examples

</>复制代码

  1. remove([1, 2, 3, 4], function ($n) {
  2. return ($n % 2) === 0;
  3. });
  4. // [0 => 1, 2 => 3]
tail

返回数组中的所有元素,第一个元素除外。

</>复制代码

  1. function tail($items)
  2. {
  3. return count($items) > 1 ? array_slice($items, 1) : $items;
  4. }

Examples

</>复制代码

  1. tail([1, 2, 3]); // [2, 3]
take

返回一个数组,其中从开头删除了n个元素。

</>复制代码

  1. function take($items, $n = 1)
  2. {
  3. return array_slice($items, 0, $n);
  4. }

Examples

</>复制代码

  1. take([1, 2, 3], 5); // [1, 2, 3]
  2. take([1, 2, 3, 4, 5], 2); // [1, 2]
without

筛选出给定值之外的数组元素。

</>复制代码

  1. function without($items, ...$params)
  2. {
  3. return array_values(array_diff($items, $params));
  4. }

Examples

</>复制代码

  1. without([2, 1, 2, 3, 5, 8], 1, 2, 8); // [3, 5]
orderBy

按键名对数组或对象的集合进行排序。

</>复制代码

  1. function orderBy($items, $attr, $order)
  2. {
  3. $sortedItems = [];
  4. foreach ($items as $item) {
  5. $key = is_object($item) ? $item->{$attr} : $item[$attr];
  6. $sortedItems[$key] = $item;
  7. }
  8. if ($order === "desc") {
  9. krsort($sortedItems);
  10. } else {
  11. ksort($sortedItems);
  12. }
  13. return array_values($sortedItems);
  14. }

Examples

</>复制代码

  1. orderBy(
  2. [
  3. ["id" => 2, "name" => "Joy"],
  4. ["id" => 3, "name" => "Khaja"],
  5. ["id" => 1, "name" => "Raja"]
  6. ],
  7. "id",
  8. "desc"
  9. ); // [["id" => 3, "name" => "Khaja"], ["id" => 2, "name" => "Joy"], ["id" => 1, "name" => "Raja"]]

相关文章:
30秒的PHP代码片段(2)数学 - Math
30秒的PHP代码片段(3)字符串-String & 函数-Function

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/30083.html

相关文章

  • 30秒的PHP代码片段(2)数学 - Math

    摘要:本文来自开源项目点我跳转秒的代码片段精选的有用片段集合,您可以在秒或更短的时间内理解这些片段。检查提供的整数是否是素数。约等于检查两个数字是否近似相等。否则,返回该范围内最近的数字。相关文章秒的代码片段数组秒的代码片段字符串函数 本文来自GitHub开源项目 点我跳转 30秒的PHP代码片段 showImg(https://segmentfault.com/img/bVbnR1I?w=...

    mcterry 评论0 收藏0
  • 30秒的PHP代码片段(3)字符串-String & 函数-Function

    摘要:返回给定字符串中的元音数。使用正则表达式来计算字符串中元音的数量。对字符串的第一个字母进行无头化,然后将其与字符串的其他部分相加。使用查找字符串中第一个出现的子字符串的位置。相关文章秒的代码片段数组秒的代码片段数学 本文来自GitHub开源项目 点我跳转 30秒的PHP代码片段 showImg(https://segmentfault.com/img/bVbnR1I?w=2800&h=...

    BlackMass 评论0 收藏0
  • Day18 - Reduce、Map混合使用计算时分秒

    摘要:混合使用计算时分秒本文出自从零到壹全栈部落作者黎跃春追时间的人简介是推出的一个天挑战。完整中文版指南及视频教程在从零到壹全栈部落。效果图第天挑战的内容主要是如何将一系列的加起来,最终计算总时间,总时间用时分秒显示。 Day18 - Reduce、Map混合使用计算时分秒 本文出自:从零到壹全栈部落作者:©黎跃春-追时间的人 简介:JavaScript30 是 Wes Bos 推出的一个...

    raoyi 评论0 收藏0
  • 30秒就能理解的 Javascript 代码片段 --- Array

    摘要:而这个秒就能理解的代码片段,摒弃了许多不必要的代码,只实现了最核心的部分,不像和那样,考虑参数边界值问题,例如,参数的类型是否符合预期等。使用根据断言函数对数组进行过滤,返回条件为真值的对象。 之前翻译过一篇文章,《我喜欢的5个编程技巧》,里面的一个技巧是借鉴一个网站的代码片段,好奇的小手点下链接后,发现是一个有 47000 多star的仓库,30-seconds-of-code。 仓...

    fox_soyoung 评论0 收藏0
  • 45 个实用的 JavaScript 技巧、窍门和最佳实践

    摘要:使用闭包实现私有变量译者添加未在构造函数中初始化的属性在语句结尾处使用分号在语句结尾处使用分号是一个很好的实践。总结我知道还有很多其他的技巧,窍门和最佳实践,所以如果你有其他想要添加或者对我分享的这些有反馈或者纠正,请在评论中指出。 showImg(http://segmentfault.com/img/bVbJnR); 如你所知,JavaScript是世界上第一的编程语言(编者注:2...

    魏宪会 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<