专栏名称: SegmentFault思否
SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。
目录
相关文章推荐
OSC开源社区  ·  RAG市场的2024:随需而变,从狂热到理性 ·  18 小时前  
码农翻身  ·  漫画 | 为什么大家都愿意进入外企? ·  昨天  
程序员的那些事  ·  印度把 DeepSeek ... ·  2 天前  
OSC开源社区  ·  宇树王兴兴早年创业分享引围观 ·  3 天前  
程序员的那些事  ·  惊!小偷“零元购”后竟向 DeepSeek ... ·  3 天前  
51好读  ›  专栏  ›  SegmentFault思否

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

SegmentFault思否  · 公众号  · 程序员  · 2019-02-25 08:00

正文

本文来自GitHub开源项目:https://github.com/appzcoder/30-seconds-of-php-code#all 精选有用的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.    







请到「今天看啥」查看全文


推荐文章
OSC开源社区  ·  RAG市场的2024:随需而变,从狂热到理性
18 小时前
OSC开源社区  ·  宇树王兴兴早年创业分享引围观
3 天前
马哥Linux运维  ·  15个Linux文件传输命令
8 年前
经典人生感悟  ·  懂你 + 疼你 + 容你 = 最真的感情!
7 年前
i黑马  ·  互联网大佬们的镀金时代
7 年前