jalismrs/common.helpers.array

添加数组辅助方法

此包的官方仓库似乎已消失,因此该包已被冻结。

1.1.2 2021-03-04 10:49 UTC

This package is auto-updated.

Last update: 2024-05-04 17:42:14 UTC


README

添加数组辅助方法

测试

phpunitvendor/bin/phpunit

覆盖率报告将在 var/coverage 中提供

使用

arrayUnique

use Jalismrs\Common\Helpers\ArrayHelpers;

$value1 = new stdClass();

$input = [
    'key1' => $value1,
    'value2',
    $value1,
    'key2' => 'value2',
];

$output = ArrayHelpers::arrayUnique(
    $input,
);

/**
 * [
 *   'key1' => $value1,
 *   'value2',
 * ]
 */

pluckOne

use Jalismrs\Common\Helpers\ArrayHelpers;

$input = [
    'key' => [
        'property1' => 'property1value1',
        'property2' => 'property2value1',
    ],
    [
        'property1' => 'property1value2',
        'property2' => 'property2value2',
    ],
];

$output = ArrayHelpers::pluckOne(
    $input,
    'property1',
);

/**
 * [
 *   'key' => 'property1value1',
 *   'property1value2',
 * ]
 */

pluckMany

use Jalismrs\Common\Helpers\ArrayHelpers;

$input = [
    'key' => [
        'property1' => 'property1value1',
        'property2' => 'property2value1',
        'property3' => 'property3value1',
    ],
    [
        'property1' => 'property1value2',
        'property2' => 'property2value2',
        'property3' => 'property3value2',
    ],
];

$output = ArrayHelpers::pluckMany(
    $input,
    [
        'property1',
        'property3',
    ],
);

/**
 * [
 *   'key' => [
 *     'property1' => 'property1value1',
 *     'property3' => 'property3value1',
 *   ],
 *   [
 *     'property1' => 'property1value2',
 *     'property3' => 'property3value2',
 *   ],
 * ]
 */

split

use Jalismrs\Common\Helpers\ArrayHelpers;

$input = [
    42,
    -69,
];

$output = ArrayHelpers::split(
    $input,
    static function(
        int $value
    ): bool {
        return $value < 10;
    },
);

/**
 * [
 *   ArrayHelpers::SPLIT_MATCHES     => [
 *     -69
 *   ],
 *   ArrayHelpers::SPLIT_NOT_MATCHES => [
 *     42,
 *   ],
 * ]
 */