zheltikov / php-arrays
1.2.0
2021-07-28 20:25 UTC
Requires
- php: ^7.4
- zheltikov/php-exceptions: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-29 03:30:08 UTC
README
这是一个PHP库,它提供的数组比原生PHP数组更加强大。
安装
要安装此库,请使用Composer
$ composer require zheltikov/php-arrays
用法
此库提供三个更优的数组类
Vec
是一个有序的、可迭代的 数据结构。Keyset
是一个无重复项的有序数据结构。它只能包含string
或int
类型的值。Dict
是一个有序的键值数据结构。键必须是string
或int
。
这些数组类都实现了 ArrayAccess
、Countable
和 Iterator
接口,以便于使用。这些数组类可以使用 AnyArray
接口进行类型指定,它们都实现了该接口。
要创建这些数组之一的新实例,可以使用它们的静态方法 create()
,可选地传递一个 iterable
作为初始数据。
要获取一个普通的PHP数组,可以使用它们的 toArray()
实例方法。
还有一些简写创建函数,它们是静态 create()
方法的包装器
vec()
keyset()
dict()
示例!
<?php require_once(__DIR__ . '/../vendor/autoload.php'); use Zheltikov\Arrays\{Vec, Keyset, Dict}; use function Zheltikov\Arrays\{vec, keyset, dict}; /************************************* Vec ************************************/ // Creating a vec $items = vec(['a', 'b', 'c']); // Accessing items by index $items[0]; // 'a' $items[3]; // throws `OutOfBoundsException` // Modifying items $items[0] = 'xx'; // vec(['xx', 'b', 'c']) $items[] = 'd'; // vec(['xx', 'b', 'c', 'd']) // Getting the length count($items); // 4 $items->count(); // 4 // Seeing if a vec contains a value or index $items->contains('a'); // true $items->containsKey(2); // true // Iterating foreach ($items as $item) { echo $item; } // Iterating with the index foreach ($items as $index => $item) { echo $index; // e.g. 0 echo $item; // e.g. 'a' } // Equality checks. Elements are recursively compared with === Vec::equal(vec([1]), vec([1])); // true Vec::equal(vec([1, 2]), vec([2, 1])); // false // Combining vecs Vec::concat(vec([1]), vec([2, 3])); // vec([1, 2, 3]) // Removing items at an index $items = vec(['a', 'b', 'c']); $n = 1; unset($items[$n]); // vec(['a', 'c']) // Converting from an iterable vec(keyset([10, 11])); // vec([10, 11]) vec([20, 21]); // vec([20, 21]) vec(dict(['key1' => 'value1'])); // vec(['value1']) // Type checks $items instanceof Vec; // true /*********************************** Keyset ***********************************/ // Creating a keyset $items = keyset(['a', 'b', 'c']); // Checking if a keyset contains a value $items->contains('a'); // true // Adding/removing items $items[] = 'd'; // keyset(['a', 'b', 'c', 'd']) $items[] = 'a'; // keyset(['a', 'b', 'c', 'd']) unset($items['b']); // keyset(['a', 'c', 'd']) // Getting the length count($items); // 3 $items->count(); // 3 // Iterating foreach ($items as $item) { echo $item; } // Equality checks. `match` returns false if the order does not match. Keyset::equal(keyset([1]), keyset([1])); // true Keyset::match(keyset([1, 2]), keyset([2, 1])); // false Keyset::equal(keyset([1, 2]), keyset([2, 1])); // true // Combining keysets Keyset::union(keyset([1, 2]), keyset([2, 3])); // keyset([1, 2, 3]) // Converting from an iterable keyset(vec([1, 2, 1])); // keyset([1, 2]) keyset([20, 21]); // keyset([20, 21]) keyset(dict(['key1' => 'value1'])); // keyset(['value1']) // Type checks $items instanceof Keyset; // true /************************************ Dict ************************************/ // Creating a dict $items = dict(['a' => 1, 'b' => 3]); // Accessing items by key $items['a']; // 1 $items['foo']; // throws OutOfBoundsException // Inserting, updating or removing values in a dict $items['a'] = 42; // dict(['a' => 42, 'b' => 3]) $items['z'] = 100; // dict(['a' => 42, 'b' => 3, 'z' => 100]) unset($items['b']); // dict(['a' => 42, 'z' => 100]) // Getting the keys Vec::keys(dict(['a' => 1, 'b' => 3])); // vec(['a', 'b']) // Getting the values vec(dict(['a' => 1, 'b' => 3])); // vec([1, 3]) // Getting the length. count($items); // 2 $items->count(); // 2 // Checking if a dict contains a key or value $items->containsKey('a'); // true $items->contains(3); // true // Iterating values foreach ($items as $value) { echo $value; // e.g. 1 } // Iterating keys and values foreach ($items as $key => $value) { echo $key; // e.g. 'a' echo $value; // e.g. 1 } // Equality checks. `match` returns false if the order does not match. Dict::equal(dict(), dict()); // true Dict::match(dict([0 => 10, 1 => 11]), dict([1 => 11, 0 => 10])); // false Dict::equal(dict([0 => 10, 1 => 11]), dict([1 => 11, 0 => 10])); // true // Combining dicts (last item wins) Dict::merge(dict([10 => 1, 20 => 2]), dict([30 => 3, 10 => 0])); // dict([10 => 0, 20 => 2, 30 => 3]) // Converting from an iterable dict(vec(['a', 'b'])); // dict([0 => 'a', 1 => 'b']) dict(['a' => 5]); // dict(['a' => 5]) // Type checks. $items instanceof Dict; // true