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

本文來自GitHub開源項目

點我跳轉php

30秒的PHP代碼片斷

clipboard.png

精選的有用PHP片斷集合,您能夠在30秒或更短的時間內理解這些片斷。

排列

all

若是所提供的函數返回 true 的數量等於數組中成員數量的總和,則函數返回 true,不然返回 falsegit

function all($items, $func)
{
    return count(array_filter($items, $func)) === count($items);
}

Examplesgithub

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

any

若是提供的函數對數組中的至少一個元素返回true,則返回true,不然返回falsesegmentfault

function any($items, $func)
{
    return count(array_filter($items, $func)) > 0;
}

Examples數組

any([1, 2, 3, 4], function ($item) {
    return $item < 2;
}); // true

deepFlatten(深度平鋪數組)

將多維數組轉爲一維數組app

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個元素。spa

function drop($items, $n = 1)
{
    return array_slice($items, $n);
}

Examplescode

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

相關文章
相關標籤/搜索