johnykvsky/arrayhelper

ArrayHelper based on KohanaPHP::Arr

0.2.0 2020-06-03 21:10 UTC

This package is auto-updated.

Last update: 2024-11-15 03:53:07 UTC


README

Latest Version on Packagist Software License Total Downloads Build Status

ArrayHelper - based on KohanaPHP Arr class. Proper merge, without surprises - see usage for example.

Install

Via Composer

$ composer require johnykvsky/arrayhelper

Should work fine on PHP 5.6, but I didn't check that. Just change required PHP version in composer.json and maybe remove dev packages. And type hinting from code.

Usage

use johnykvsky\Utils\ArrayHelper;
$array = array('johny'=>array('age'=>30,'weight'=>70),'chris'=>array('height'=>170));
$array2 = array('hobby'=>array(array('music'=>'rock')));

$result = ArrayHelper::merge($array, $array2); //merge arrays

$array3 = array('chris'=>array('weight'=>70));
$array1 = array('fragment' => 'x', 'attributes' => array('title' => 'X', 'class' => array('a', 'b')));
$array2 = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('c', 'd')));

$result = ArrayHelper::merge($array1, $array2); //deep merge, fragment = y / attributes: title = Y / class = a,b,c,d
$result = ArrayHelper::merge($array1, $array2, false); //non-deep, fragment = y / attributes title = Y / class = c,d

//potential problem with array_merge_recursive:
$incorrect = array_merge_recursive($array1, $array2);
//result is array('fragment' => array('x', 'y'), 'attributes' => array('title' => array('X', 'Y'), 'class' => array('a', 'b', 'c', 'd'))).

$array4 = ['foo' , 'arj','pak'];
$array5 = ['foo' => 'tar', 'baz'=>'zip'];
//combine - get all values from array5 into array4
$result = ArrayHelper::combine($array4, $array5);
//result is ['foo','arj','pak','tar','zip']

$foo = ArrayHelper::firstValue($array5); // tar
$baz = ArrayHelper::lastValue($array5); // zip

$bar = ArrayHelper::flatten($array1); // ['x','X','a','b']

Testing

$ composer test

Code checking

$ composer phpstan
$ composer phpstan-max

Security

If you discover any security related issues, please email johnykvsky@protonmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.