gupalo / arrayutils
Array utils
Installs: 8 169
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
- symfony/property-access: ^5.0|^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
Install
composer require gupalo/arrayutils
Features
All code is covered by tests. Look at tests to understand better how it's working.
All methods are static.
Items can be iterable (array, \Generator, ...) of one of:
- array
- object with public properties or getters (anything Symfony PropertyAccessor can get)
ArrayAggregator
max(items, field)
: max field value among itemssum(items, field)
: sum of field values of itemsratio(items, field, field2
:"sum by field"/"sum by field2"
maxRatio(items, field, field2)
: max among"field value"/"field value2"
ArrayComparer
Comparing is smart - if compared values are float, they are rounded with precision = 8.
compareOne(a1, a2, keys)
: check that each(string)a1[key] === (string)a2[key]
for each of keys. returns "patch" how to go from a1 to a2.null
value means that we need to unset this key.compare(a1, a2, keys)
a1 and a2 are expected to be array of arrays. returns patch how what's changed in a2 compared to a1 - array with keys:created
: which items should be created and their valuesupdated
: which items should be updated and updated fieldsremoved
: which items (ids) should be deleted
compareFlat(a1, a2)
similar tocompare
but a1 and a2 are considered scalars and are compared fully, not by fields
ArrayFactory
createKeys(keys, value)
: create array with these keys and valuecreateDictionary(a, keyColumn, valueColumn)
: similar toarray_column(a, valueColumn, keyColumn)
but doesn't require values present in all items (?? null
)
ArrayKeysHelper
index($a, array|string $keys = 'id')
: create indexed array where index may contain several fieldsindexAndGroup($a, array|string $keys = 'id')
: create indexed array but allow several items with same indexfill($a, $keys, $defaultValue = null)
: ensure that all keys exist in array and all others are missingfilter($a, $keys, $createMissingKeys = true)
: get these keys from arrayunset($a, $keys)
: unset these keys from each array item
ArrayRandom
pick(array $items)
: select random value from arraypickMultiple(array $items, int $count = 1, bool $preserveKeys = false, mixed $default = [])
: select multiple ($count) random values from array
ArrayTable
arrayToKeyValues(array $data)
: covert table (like TSV; first item is header with keys) to array with named keys
ArrayUniquer
values(array $a)
: uniq -> valuesnotNullValues(array $a)
: filter not null -> uniq -> valuescolumnValues(array $a, string $column)
: column -> uniq -> valuesmergeValues(...$a)
: merge -> uniq -> valuesmergeNotNullValues(...$a)
: merge -> filter not null -> uniq -> values
FloatArrayFactory
createPreserveKeys(array $a)
: ensure that all values are floatcreate(array $a)
: ensure that all values are float -> values
IntArrayFactory
createPreserveKeys(array $a)
: ensure that all values are intcreate(array $a)
: ensure that all values are int -> values
Balancer
Problem to solve: you have some items and need to split them to some buckets.
You may configure different strategies how to distribute your items between the buckets.
Buckets should implement BucketInterface
, balancer should implement BalancerInterface
SemiRandomBalancer
It's not fully random, you set seed so multiple runs with the same seed and same items will lead to the same result.
FairBalancer
Try to make same weighted count of items in same bucket.
Example: A/B test where you want to send 90% of users to "A" page and 10% to "B" page. You could use simple random but with low amount of users it's quite probable that you have 20 users and all will see page "A" or 5 users that go to "B". Then you'll have hard time explaining probability theory. With this balancer you may be sure that 1 of the first 10 users will go to "B" and most probably 2 of 20 users will land there.