摘要:排列如果所提供的函數返回的數量等于數組中成員數量的總和,則函數返回,否則返回。平鋪數組將數組降為一維數組根據給定的函數對數組的元素進行分組。使用給定的回調篩選數組。相關文章秒的代碼片段數學秒的代碼片段字符串函數
本文來自GitHub開源項目
點我跳轉
30秒的PHP代碼片段精選的有用PHP片段集合,您可以在30秒或更短的時間內理解這些片段。排列 all
如果所提供的函數返回 true 的數量等于數組中成員數量的總和,則函數返回 true,否則返回 false。
function all($items, $func) { return count(array_filter($items, $func)) === count($items); }
Examples
all([2, 3, 4, 5], function ($item) { return $item > 1; }); // trueany
如果提供的函數對數組中的至少一個元素返回true,則返回true,否則返回false。
function any($items, $func) { return count(array_filter($items, $func)) > 0; }
Examples
any([1, 2, 3, 4], function ($item) { return $item < 2; }); // truedeepFlatten(深度平鋪數組)
將多維數組轉為一維數組
function deepFlatten($items) { $result = []; foreach ($items as $item) { if (!is_array($item)) { $result[] = $item; } else { $result = array_merge($result, deepFlatten($item)); } } return $result; }
Examples
deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]drop
返回一個新數組,并從左側彈出n個元素。
function drop($items, $n = 1) { return array_slice($items, $n); }
Examples
drop([1, 2, 3]); // [2,3] drop([1, 2, 3], 2); // [3]findLast
返回所提供的函數為其返回的有效值(即過濾后的值)的最后一個元素的鍵值(value)。
function findLast($items, $func) { $filteredItems = array_filter($items, $func); return array_pop($filteredItems); }
Examples
findLast([1, 2, 3, 4], function ($n) { return ($n % 2) === 1; }); // 3findLastIndex
返回所提供的函數為其返回的有效值(即過濾后的值)的最后一個元素的鍵名(key)。
function findLastIndex($items, $func) { $keys = array_keys(array_filter($items, $func)); return array_pop($keys); }
Examples
findLastIndex([1, 2, 3, 4], function ($n) { return ($n % 2) === 1; }); // 2flatten(平鋪數組)
將數組降為一維數組
function flatten($items) { $result = []; foreach ($items as $item) { if (!is_array($item)) { $result[] = $item; } else { $result = array_merge($result, array_values($item)); } } return $result; }
Examples
flatten([1, [2], 3, 4]); // [1, 2, 3, 4]groupBy
根據給定的函數對數組的元素進行分組。
function groupBy($items, $func) { $group = []; foreach ($items as $item) { if ((!is_string($func) && is_callable($func)) || function_exists($func)) { $key = call_user_func($func, $item); $group[$key][] = $item; } elseif (is_object($item)) { $group[$item->{$func}][] = $item; } elseif (isset($item[$func])) { $group[$item[$func]][] = $item; } } return $group; }
Examples
groupBy(["one", "two", "three"], "strlen"); // [3 => ["one", "two"], 5 => ["three"]]hasDuplicates(查重)
檢查數組中的重復值。如果存在重復值,則返回true;如果所有值都是唯一的,則返回false。
function hasDuplicates($items) { return count($items) > count(array_unique($items)); }
Examples
hasDuplicates([1, 2, 3, 4, 5, 5]); // truehead
返回數組中的第一個元素。
function head($items) { return reset($items); }
Examples
head([1, 2, 3]); // 1last
返回數組中的最后一個元素。
function last($items) { return end($items); }
Examples
last([1, 2, 3]); // 3pluck
檢索給定鍵名的所有鍵值
function pluck($items, $key) { return array_map( function($item) use ($key) { return is_object($item) ? $item->$key : $item[$key]; }, $items); }
Examples
pluck([ ["product_id" => "prod-100", "name" => "Desk"], ["product_id" => "prod-200", "name" => "Chair"], ], "name"); // ["Desk", "Chair"]pull
修改原始數組以過濾掉指定的值。
function pull(&$items, ...$params) { $items = array_values(array_diff($items, $params)); return $items; }
Examples
$items = ["a", "b", "c", "a", "b", "c"]; pull($items, "a", "c"); // $items will be ["b", "b"]reject
使用給定的回調篩選數組。
function reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); }
Examples
reject(["Apple", "Pear", "Kiwi", "Banana"], function ($item) { return strlen($item) > 4; }); // ["Pear", "Kiwi"]remove
從給定函數返回false的數組中刪除元素。
function remove($items, $func) { $filtered = array_filter($items, $func); return array_diff_key($items, $filtered); }
Examples
remove([1, 2, 3, 4], function ($n) { return ($n % 2) === 0; }); // [0 => 1, 2 => 3]tail
返回數組中的所有元素,第一個元素除外。
function tail($items) { return count($items) > 1 ? array_slice($items, 1) : $items; }
Examples
tail([1, 2, 3]); // [2, 3]take
返回一個數組,其中從開頭刪除了n個元素。
function take($items, $n = 1) { return array_slice($items, 0, $n); }
Examples
take([1, 2, 3], 5); // [1, 2, 3] take([1, 2, 3, 4, 5], 2); // [1, 2]without
篩選出給定值之外的數組元素。
function without($items, ...$params) { return array_values(array_diff($items, $params)); }
Examples
without([2, 1, 2, 3, 5, 8], 1, 2, 8); // [3, 5]orderBy
按鍵名對數組或對象的集合進行排序。
function orderBy($items, $attr, $order) { $sortedItems = []; foreach ($items as $item) { $key = is_object($item) ? $item->{$attr} : $item[$attr]; $sortedItems[$key] = $item; } if ($order === "desc") { krsort($sortedItems); } else { ksort($sortedItems); } return array_values($sortedItems); }
Examples
orderBy( [ ["id" => 2, "name" => "Joy"], ["id" => 3, "name" => "Khaja"], ["id" => 1, "name" => "Raja"] ], "id", "desc" ); // [["id" => 3, "name" => "Khaja"], ["id" => 2, "name" => "Joy"], ["id" => 1, "name" => "Raja"]]
相關文章:
30秒的PHP代碼片段(2)數學 - Math
30秒的PHP代碼片段(3)字符串-String & 函數-Function
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://www.ezyhdfw.cn/yun/30083.html
摘要:本文來自開源項目點我跳轉秒的代碼片段精選的有用片段集合,您可以在秒或更短的時間內理解這些片段。檢查提供的整數是否是素數。約等于檢查兩個數字是否近似相等。否則,返回該范圍內最近的數字。相關文章秒的代碼片段數組秒的代碼片段字符串函數 本文來自GitHub開源項目 點我跳轉 30秒的PHP代碼片段 showImg(https://segmentfault.com/img/bVbnR1I?w=...
摘要:返回給定字符串中的元音數。使用正則表達式來計算字符串中元音的數量。對字符串的第一個字母進行無頭化,然后將其與字符串的其他部分相加。使用查找字符串中第一個出現(xiàn)的子字符串的位置。相關文章秒的代碼片段數組秒的代碼片段數學 本文來自GitHub開源項目 點我跳轉 30秒的PHP代碼片段 showImg(https://segmentfault.com/img/bVbnR1I?w=2800&h=...
摘要:混合使用計算時分秒本文出自從零到壹全棧部落作者黎躍春追時間的人簡介是推出的一個天挑戰(zhàn)。完整中文版指南及視頻教程在從零到壹全棧部落。效果圖第天挑戰(zhàn)的內容主要是如何將一系列的加起來,最終計算總時間,總時間用時分秒顯示。 Day18 - Reduce、Map混合使用計算時分秒 本文出自:從零到壹全棧部落作者:?黎躍春-追時間的人 簡介:JavaScript30 是 Wes Bos 推出的一個...
摘要:而這個秒就能理解的代碼片段,摒棄了許多不必要的代碼,只實現(xiàn)了最核心的部分,不像和那樣,考慮參數邊界值問題,例如,參數的類型是否符合預期等。使用根據斷言函數對數組進行過濾,返回條件為真值的對象。 之前翻譯過一篇文章,《我喜歡的5個編程技巧》,里面的一個技巧是借鑒一個網站的代碼片段,好奇的小手點下鏈接后,發(fā)現(xiàn)是一個有 47000 多star的倉庫,30-seconds-of-code。 倉...
摘要:使用閉包實現(xiàn)私有變量譯者添加未在構造函數中初始化的屬性在語句結尾處使用分號在語句結尾處使用分號是一個很好的實踐??偨Y我知道還有很多其他的技巧,竅門和最佳實踐,所以如果你有其他想要添加或者對我分享的這些有反饋或者糾正,請在評論中指出。 showImg(http://segmentfault.com/img/bVbJnR); 如你所知,JavaScript是世界上第一的編程語言(編者注:2...
閱讀 2259·2021-09-06 15:02
閱讀 1896·2021-08-13 15:02
閱讀 2450·2019-08-29 14:14
閱讀 1532·2019-08-26 13:55
閱讀 642·2019-08-26 13:46
閱讀 3491·2019-08-26 11:41
閱讀 634·2019-08-26 10:27
閱讀 3376·2019-08-23 15:28