Laravel-Arr类

作用

Illuminate\Support\Arr是一个数组帮助类,便于使用。

函数解析

<?php

namespace Illuminate\Support;

use ArrayAccess;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;

class Arr
{
   
    use Macroable;

    /**
     * Determine whether the given value is array accessible.
     *
     * @param  mixed  $value
     * @return bool
     */
    //验证这个给定的值是否是可获取的, 如数组或者继承ArrayAccess的对象
    public static function accessible($value)
    {
   
        return is_array($value) || $value instanceof ArrayAccess;
    }

    /**
     * Add an element to an array using "dot" notation if it doesn't exist.
     *
     * @param  array   $array
     * @param  string  $key
     * @param  mixed   $value
     * @return array
     */
    // 对数组添加一个元素, 如果不存在的话,最后返回这个数组
    public static function add($array, $key, $value)
    {
   
        if (is_null(static::get($array, $key))) {
   
            static::set($array, $key, $value);
        }

        return $array;
    }

    /**
     * Collapse an array of arrays into a single array.
     *
     * @param  array  $array
     * @return array
     */
    //对二维数组进行折叠为乐一维数组
    public static function collapse($array)
    {
   
        $results = [];

        foreach ($array as $values) {
   
            if ($values instanceof Collection) {
   
                $values = $values->all();
            } elseif (! is_array($values)) {
   
                continue;
            }

            $results[] = $values;
        }

        return array_merge([], ...$results);
    }

    /**
     * Cross join the given arrays, returning all possible permutations.
     *
     * @param  array  ...$arrays
     * @return array
     */
    //对数组元素取交集, 参数必须是数组
    public static function crossJoin(...$arrays)
    {
   
        $results = [[]];

        foreach ($arrays as $index => $array) {
   
            $append = [];

            foreach ($results as $product) {
   
                foreach ($array as $item) {
   
                    $product[$index] = $item;

                    $append[] = $product;
                }
            }

            $results = $append;
        }

        return $results;
    }

    /**
     * Divide an array into two arrays. One with keys and the other with values.
     *
     * @param  array  $array
     * @return array
     */
    //获取数组的键和值, 分别作为数组
    public static function divide($array)
    {
   
        return [array_keys($array), array_values($array)];
    }

    /**
     * Flatten a multi-dimensional associative array with dots.
     *
     * @param  array   $array
     * @param  string  $prepend
     * @return array
     */
    //将多维数组使用点构建成键, 作为一个数组返回
    public static function dot($array, $prepend = '')
    {
   
        $results = [];

        foreach ($array as $key => $value) {
   
            if (is_array($value) && ! empty($value)) {
   
                $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
            } else {
   
                $results[$prepend.$key] = $value;
            }
        }

        return $results;
    }

    /**
     * Get all of the given array except for a specified array of keys.
     *
     * @param  array  $array
     * @param  array|string  $keys
     * @return array
     */
    //获取数组除了$keys之外的元素
    public static function except($array, $keys)
    {
   
        static::forget($array, $keys);

        return $array;
    }

    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值