belca/support-array

为任何项目中的数组操作提供辅助PHP函数。

v1.0 2020-04-07 19:07 UTC

This package is auto-updated.

Last update: 2024-09-08 05:08:47 UTC


README

文档适用于版本1.0。

Belca\Support\Arr类包含用于处理包含标准数据(数据不使用特殊存储规则)的数组的函数。尽管你可以按需使用它们。

一些示例

use Belca\Support\Arr;

// Determine whether an array is filled with integer keys
$result = Arr::isIntKeys([1, 2, 3, 4, 5]); // Output: true
$result = Arr::isIntKeys([1, 2, 'three' => 3, 4, 5]); // Output: false

// Check whether the first and last key of an array are integers.
$result = Arr::isFirstLastWithIntKeys([1, 2, 3, 4, 5]); // Output: true
$result = Arr::isFirstLastWithIntKeys([1, 2, 3, 4, 'five' => 5]); // Output: false

// Take a first existing value of your array
$result = Arr::firstExists(null, null, 'value'); // Output: 'value'

阅读文档以了解其他函数。

安装

您必须安装PHP 7.0及更高版本和Composer。

安装包。查看示例

composer require belca/support-array:1.*

在您的类中使用Belca\Support\Arr

use Belca\Support\Arr;

// Your code

$array = Arr::trim($array); // or another function

// or
$array = \Belca\Support\Arr::trim($array); // using the full path

现在您可以使用该包的所有函数。

数组函数

Arr::concatArray

Arr::concatArray(&$source, $array, $replace = true) : void

向源数组的末尾添加带有新键的新值。

如果$replace为'true',则所有具有相同键的现有值将被新值替换。

与array_merge()不同,该函数不会创建也不会返回一个新数组,它直接在源数组上操作。

与array_merge()不同,当使用整数键时,它将新数组添加到第一个数组中,而Arr::concatArray()将相同的整数键替换为关联键。

函数根据给定的$replace参数执行$array1 += $array2$array2 + $array1操作。

参数

  • &$source - 源数组;
  • $array - 要连接的数组;
  • $replace - 替换值。如果$replacetrue,则所有具有相同键的现有值将被替换。

示例 #1: 向数组中添加新值并替换具有相同键的值。数组具有整数键。

$source = [1, 2, 3, 4, 5, 6];
$array = [6, 7, 8, 9, 10, 11, 12];

Arr::concatArray($source, $array);

// Output $source: [6, 7, 8, 9, 10, 11, 12];

源数组的头6个值被新值替换。还添加了一个新值:12。这是因为源数组的键与第二个数组相匹配。

示例 #2: 向数组中添加新值并替换具有相同键的值。第二个数组的键有偏移。

$source = [1, 2, 3, 4, 5, 6];
$array = [6 => 6, 7, 8, 9, 10, 11, 12];

Arr::concatArray($source, $array);

// Output $source: [1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12];

在这个例子中,我们得到了必要的结果,函数将新值连接到源数组。这有时可能很有用。

在下一个示例中,我们将使用关联数组,其中具有相同键的值将被替换。

示例 #3: 向数组中添加新值并替换具有相同键的旧值。

$source = ['key1' => 1, 'key2' => 2];
$newValues = ['key2' => 3, 'key3' => 4];

Arr::concatArray($source, $newValues);

// Output $source: ['key1' => 1, 'key2' => 3, 'key3' => 4];

然而,替换源数组的值不一定总是有用的。您可能只能添加尚未在源数组中存在的值。

请看此示例。

示例 4: 只向数组中添加新值。

$source = ['key1' => 1, 'key2' => 2];
$newValues = ['key2' => 3, 'key3' => 4];

Arr::concatArray($source, $newValues, false);

// Output $source: ['key1' => 1, 'key2' => 2, 'key3' => 4];

您可能注意到该函数没有返回结果,它直接处理源数组。

另请参阅:

Arr::firstExists

Arr::firstExists(...$values): mixed

返回第一个存在的值或返回'null'。

$result = Arr::firstExists(null, null, false, 0, '', true); // Output: false
$result = Arr::firstExists(null, null, 0, '', 'value', true, []); // Output: 0
$result = Arr::firstExists(null, null, [], '', 'value', true); // Output: []

Arr::firstNotEmpty

Arr::firstNotEmpty(...$values): mixed

返回第一个非空值,或者返回 'null'。

$result = Arr::firstNotEmpty(null, null, false, 0, '', true); // Output: true
$result = Arr::firstNotEmpty(null, null, false, 0, '', 'value', true, []); // Output: 'value'

Arr::isArrayWithIntKeys

Arr::isArrayWithIntKeys(array $array) : boolean

检查数组中是否有整数键。用于检测非关联数组。如果数组中的所有键都是整数,则返回 'true'。

空数组不是整数数组,因为其键无法检测。

$array1 = [1, 2, 3, 4, 5, 6, 7, 8, 10];
$result1 = Arr::isArrayWithIntKeys($array1); // true

$array2 = []; // false
$result2 = Arr::isArrayWithIntKeys($array2); // false, потому что пустой массив

$array3 = ['1' => 1, 2, 3, '4' => 4];
$result3 = Arr::isArrayWithIntKeys($array4); // true, потому что числа в строке преобразованы в integer

$array4 = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1];
$result4 = Arr::isArrayWithIntKeys($array5); // false

另请参阅:

Arr::isFirstLastWithIntKeys

Arr::isFirstLastWithIntKeys(array $array) : boolean

检查第一个值和最后一个值是否有整数键。如果它们是整数,则返回 'true'。用于简单检测非关联数组。

此函数类似于 isArrayWithIntKeys(),但键必须属于以下类型之一:字符串键或整数键。

空数组不是整数数组,因为其键无法检测。

$array1 = [1, 2, 3, 4, 5, 6, 7, 8, 10];
$result1 = Arr::isFirstLastWithIntKeys($array1); // true

$array2 = [];
$result2 = Arr::isFirstLastWithIntKeys($array2); // false, because the array is empty

$array3 = ['1' => 1, 2, 3, '4' => 4];
$result3 = Arr::isFirstLastWithIntKeys($array4); // true, because the number in the keys converted to the integer

$array4 = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1];
$result4 = Arr::isFirstLastWithIntKeys($array5); // true, because the first and the last keys are the integer

$array5 = [50 => 1, 'a2' => 3, 'a3' => 4, 'one' => 1];
$result5 = Arr::isFirstLastWithIntKeys($array6); // false, because the last key is the string

Arr::isArrayWithIntKeys() (Arr::isIntKeys()) 相比,后者可能扫描数组的所有值,此函数仅检查第一个和最后一个键,因此执行更快。

另请参阅:

Arr::isIntKeys

Arr::isIntKeys(array $array) : boolean

Arr::isArrayWithIntKeys() 的别名。

$normalArray = [1, 2, 3, 4, 5, 6, 7, 8, 10];
$result1 = Arr::isIntKeys($normalArray); // true

$badArray = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1];
$result2 = Arr::isIntKeys($badArray); // false

Arr::last

Arr::last(&$array) : mixed

返回给定数组的最后一个元素。保存数组位置的指针。

$array = [5 => 1, 2, 3, 4, 5];

$last = Arr::last($array); // Output: 5

Arr::pushArray

Arr::pushArray(&$array, ...$array) : void

将其他数组的值连接到源数组。如果值相等,则替换字符串键的值,数字键的值将附加到源数组。

$source = [1, 2, 3, 'key1' => 1, 'key2' => 2, 'key3' => 3];
$array1 = [4, 5, 6];
$array2 = [1, 2, 3, 'key1' => 10];

Arr::pushArray($source, $array1, $array2);

// Output $source:
// [1, 2, 3, 'key1' => 10, 'key2' => 2, 'key3' => 3, 4, 5, 6, 1, 2, 3]

查看结果。来自 $array2 的 1, 2, 3 值被添加到源数组中,但源数组中已有这些值,因此添加了重复值。

如果您想在数组中不包含相同的值,请使用 array_unique() 获取唯一的值。该函数将应用于所有键,甚至字符串键,其中不同的键可能保持相同的值。

另请参阅:

Arr::removeArrays

Arr::removeArrays($array, $resetIndex = false) : array

从数组中删除嵌套数组(子数组)。如果 $resetIndex 为 'true',则重置数组的键。

参数

  • $array - 数组;
  • $resetIndex - 重置数组的键。如果 $resetIndextrue,则重置数组的键。

示例 1:删除内部数组。

$array = [
    1,
    2,
    3,
    'four' => 4,
    'five' => 5,
    'matrix' => [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ],
    7,
    'eight' => 8,
    9,
    'symbols' => ['a', 'b', 'c'],
    'object' => new stdClass(),
];

$result = Arr::removeArrays($array);

// Output: [
//    1,
//    2,
//    3,
//    'four' => 4,
//    'five' => 5,
//    7,
//    'eight' => 8,
//    9,
//    'object' => new stdClass(),
// ];

在示例中,删除了数组中的所有数组,并保留了其他值。

有时您需要重置数组键,如下所示。

示例 2:删除内部数组并重置数组键。

$array = [
    1,
    2,
    3,
    'four' => 4,
    'five' => 5,
    'matrix' => [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ],
    7,
    'eight' => 8,
    9,
    'symbols' => ['a', 'b', 'c'],
    'object' => new stdClass(),
];

$result = Arr::removeArrays($array, true);

// Output: [1, 2, 3, 4, 5, 7, 8, 9, new stdClass()];

Arr::removeEmpty

Arr::removeEmpty(array $array) : array

删除数组中的每个空值。使用 'empty' 函数。保留数组键。

$array4 = [1, 2, null, '', [], new stdClass, false, 0];

$result = Arr::removeEmpty($array);

// Output: [1, 2, 5 => new stdClass];

Arr::removeEmptyRecurcive

Arr::removeEmptyRecurcive(array $array, boolean $resetIndex = true) : array

递归删除多维数组中的空值。如果函数接收的不是数组,则返回未更改的值。

参数

  • $array - 任何数组;
  • $resetIndex - 重置数组的键。如果 $resetIndextrue,则重置每个数组中的整数键,包括手动设置的键。

示例 #1. 递归删除空值并重置数组键。

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    'a3' => [
        1,
        2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeEmptyRecurcive($array);

// Output:
// [
//    1 => [0 => 1, 1 => 2, 2 => [1, 2, 3, 4], 3 => 4],
//    2 => -2,
//    'a3' => [
//         0 => 1,
//         1 => 2,
//         'a3.3' => [0 => 1, 1 => 2, 2 => 3],
//    ],
// ]

在示例中,您可以看到重置键只存在于整数键的数组中。因此,数组的基本键和 $arrar['a3'] 值的键没有改变,其他键被重置。

示例 2. 递归删除空值而不重置键

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    'a3' => [
        1,
        2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeEmptyRecurcive($array, false);

// Output:
// [
//    1 => [1, 2, 3 => [1, 2, 3, 4], 4 => 4],
//    2 => -2,
//    'a3' => [
//         0 => 1,
//         1 => 2,
//         'a3.3' => [1 => 1, 2 => 2, 3 => 3],
//    ],
// ]

在示例中,键没有改变,因为 $resetIndex 被设置为 false

另请参阅:

Arr::removeNotScalar

Arr::removeNotScalar(array $array) : array

移除数组中值为 'null' 或其他非标量值的每个值。标量值是包含整数、浮点数、字符串或布尔值的值。

$array = [1, 2, null, '', [], new stdClass, false, 0];
$result = Arr::removeNotScalar($array);

// Output: [0 => 1, 1 => 2, 3 => '', 6 => false, 7 => 0];

Arr::removeNull

Arr::removeNull(array $array) : array

移除数组中值为 'null' 的每个值。使用函数 'is_null'。保存数组的键。

$array = [1, 2, null, '', [], new stdClass, false, 0];

$result = Arr::removeNull($array);  

// Output: [1, 2, 3 => '', [], new stdClass, false, 0];

Arr::removeNullRecurcive

Arr::removeNullRecurcive(array $array, boolean $resetIndex = true) : array

递归删除多维数组中的 null 值。

参数

  • $array - 任何数组;
  • $resetIndex - 数组的键重置。如果 $resetIndextrue,则重置每个数组的整数键,包括手动设置的键。如果一个数组有一个或多个非整数键,则保存键。如果函数接收的不是数组,则返回未更改的值。

示例 1:删除具有 'null' 的值并重置键。

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    4 => [
        1 => 1,
        2 => 2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeNullRecurcive($array);

// Output:
// [
//    0 => [0 => 1, 1 => 2, 2 => [1, 2, 3, 4, []], 3 => 4, 4 => ''],
//    1 => -2,
//    2 => [
//         1 => 1,
//         2 => 2,
//         'a3.3' => [0 => 1, 1 => 2, 2 => 3],
//    ],
//    3 => '',
//    4 => 0,
//    5 => false,
// ]

在示例中,执行重置具有整数键的数组的键。因此,$array[2] 的键没有改变,但值的索引已更改。

示例 2:删除具有 'null' 的值而不重置键。

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    4 => [
        1 => 1,
        2 => 2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeNullRecurcive($array, false);

// Output:
// [
//    1 => [1, 2, 3 => [1, 2, 3, 4, []], 4 => 4, 5 => ''],
//    2 => -2,
//    4 => [
//         1 => 1,
//         2 => 2,
//         'a3.3' => [1 => 1, 2 => 2, 3 => 3],
//    ],
//    6 => '',
//    7 => 0,
//    8 => false,
// ]

另请参阅:

Arr::trim

Arr::trim(array $array) : array

修剪数组中的字符串值。保存数组的键。该函数不处理嵌套数组。

$array = [
    ' value ',
    'trim',
    'one ',
    ' two',
    '   three    ',
    1,
    2,
    'string',
    null,
    ['  no trim  '],
];

$result = Arr::trim($array);

// Output: ['value', 'trim', 'one', 'two', 'three', 1, 2, 'string', null, ['  no trim  ']];

Arr::unset

Arr::unset($array, ...$indexes) : array

通过给定的键移除数组的值,而不更改数组的键。返回一个新数组。

$array = [
    1,
    2,
    3,
    'four' => 4,
    'five' => 5,
    'matrix' => [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ],
    7,
    'eight' => 8,
    9,
    'symbols' => ['a', 'b', 'c'],
    'object' => new stdClass(),
];

$output = Arr::unset($array, 0, 'four', 'eight', 4);
// or
$output = Arr::unset($array, [0, 'four', 'eight', 4]);
// or
$output = Arr::unset($array, [0, 'four'], ['eight', 4]);
// or
$output = Arr::unset($array, [0, 'four'], [['eight'], [4], []]);

// Output:
// [
//    1 => 2,
//    2 => 3,
//   'five' => 5,
//    'matrix' => [
//        [1, 2, 3],
//        [4, 5, 6],
//         [7, 8, 9],
//    ],
//    3 => 7,
//    'symbols' => ['a', 'b', 'c'],
//    'object' => new stdClass(),
// ]

此函数在您需要通过知道它们的索引从数组中删除未知值时可能很有用。可以使用 array_filter()unset() 获取相同的效果,但此函数更易读且更紧凑。

Arr::unsetByReference

Arr::unsetByReference(array &$array, ...$keys): void

通过给定的键移除数组的值。该函数不返回结果。

示例 1:使用简单键删除值

$array = [
    1, 2, 3, 4, 5, 'six' => 6, 'seven' => 7, 8, 9, 'ten' => 10, 11
];

Arr::unsetByReference($array, 0, 'six', 'ten', 'unknown');

// Output $array:
// [
//    1 => 2, 3, 4, 5, 'seven' => 7, 8, 9, 11
// ]

示例 2:使用数组中的键删除值

$array = [
    1, 2, 3, 4, 5, 'six' => 6, 'seven' => 7, 8, 9, 'ten' => 10, 11
];

Arr::unsetByReference($array, [0, 1], ['six', 'ten', 'unknown']);

// Output $array:
// [
//    2 => 3, 4, 5, 'seven' => 7, 8, 9, 11
// ]

另请参阅: