亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

30秒的PHP代碼片段(1)數組 - Array

dunizb / 1037人閱讀

摘要:排列如果所提供的函數返回的數量等于數組中成員數量的總和,則函數返回,否則返回。平鋪數組將數組降為一維數組根據給定的函數對數組的元素進行分組。使用給定的回調篩選數組。相關文章秒的代碼片段數學秒的代碼片段字符串函數

本文來自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;
}); // true
any

如果提供的函數對數組中的至少一個元素返回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;
}); // true
deepFlatten(深度平鋪數組)

將多維數組轉為一維數組

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;
});
// 3
findLastIndex

返回所提供的函數為其返回的有效值(即過濾后的值)的最后一個元素的鍵名(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;
});
// 2
flatten(平鋪數組)

將數組降為一維數組

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]); // true
head

返回數組中的第一個元素。

function head($items)
{
    return reset($items);
}

Examples

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

返回數組中的最后一個元素。

function last($items)
{
    return end($items);
}

Examples

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

檢索給定鍵名的所有鍵值

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

相關文章

  • 30秒的PHP代碼片段(2)數學 - Math

    摘要:本文來自開源項目點我跳轉秒的代碼片段精選的有用片段集合,您可以在秒或更短的時間內理解這些片段。檢查提供的整數是否是素數。約等于檢查兩個數字是否近似相等。否則,返回該范圍內最近的數字。相關文章秒的代碼片段數組秒的代碼片段字符串函數 本文來自GitHub開源項目 點我跳轉 30秒的PHP代碼片段 showImg(https://segmentfault.com/img/bVbnR1I?w=...

    mcterry 評論0 收藏0
  • 30秒的PHP代碼片段(3)字符串-String & 函數-Function

    摘要:返回給定字符串中的元音數。使用正則表達式來計算字符串中元音的數量。對字符串的第一個字母進行無頭化,然后將其與字符串的其他部分相加。使用查找字符串中第一個出現(xiàn)的子字符串的位置。相關文章秒的代碼片段數組秒的代碼片段數學 本文來自GitHub開源項目 點我跳轉 30秒的PHP代碼片段 showImg(https://segmentfault.com/img/bVbnR1I?w=2800&h=...

    BlackMass 評論0 收藏0
  • Day18 - Reduce、Map混合使用計算時分秒

    摘要:混合使用計算時分秒本文出自從零到壹全棧部落作者黎躍春追時間的人簡介是推出的一個天挑戰(zhàn)。完整中文版指南及視頻教程在從零到壹全棧部落。效果圖第天挑戰(zhàn)的內容主要是如何將一系列的加起來,最終計算總時間,總時間用時分秒顯示。 Day18 - Reduce、Map混合使用計算時分秒 本文出自:從零到壹全棧部落作者:?黎躍春-追時間的人 簡介:JavaScript30 是 Wes Bos 推出的一個...

    raoyi 評論0 收藏0
  • 30秒就能理解的 Javascript 代碼片段 --- Array

    摘要:而這個秒就能理解的代碼片段,摒棄了許多不必要的代碼,只實現(xiàn)了最核心的部分,不像和那樣,考慮參數邊界值問題,例如,參數的類型是否符合預期等。使用根據斷言函數對數組進行過濾,返回條件為真值的對象。 之前翻譯過一篇文章,《我喜歡的5個編程技巧》,里面的一個技巧是借鑒一個網站的代碼片段,好奇的小手點下鏈接后,發(fā)現(xiàn)是一個有 47000 多star的倉庫,30-seconds-of-code。 倉...

    fox_soyoung 評論0 收藏0
  • 45 個實用的 JavaScript 技巧、竅門和最佳實踐

    摘要:使用閉包實現(xiàn)私有變量譯者添加未在構造函數中初始化的屬性在語句結尾處使用分號在語句結尾處使用分號是一個很好的實踐??偨Y我知道還有很多其他的技巧,竅門和最佳實踐,所以如果你有其他想要添加或者對我分享的這些有反饋或者糾正,請在評論中指出。 showImg(http://segmentfault.com/img/bVbJnR); 如你所知,JavaScript是世界上第一的編程語言(編者注:2...

    魏憲會 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<