cse/helpers-arrays

Array helpers provides extra static methods allowing you to deal with arrays more efficiently.

1.0.3 2019-04-29 04:12 UTC

This package is auto-updated.

Last update: 2024-11-19 03:32:17 UTC


README

English | Русский

ARRAYS CSE HELPERS

Travis (.org) Codecov Scrutinizer code quality

Packagist Minimum PHP Version Packagist GitHub repo size

Array helpers provides extra static methods allowing you to deal with arrays more efficiently.

Project repository: https://github.com/cs-eliseev/helpers-arrays

DEMO

$array = [
    0 => ' first 1 ',
    'key1' => false,
    1 => '',
    2 => '0',
    'key4' => null,
    3 => [
        0 => ' first 3',
        1 => false,
        'key2' => '',
        2 => '0',
        'key5' => null,
        'key6' => 12,
        3 => [],
        4 => 'first 4 '
    ],
    4 => true,
    5 => [],
    'key8' => 'first 2 '
];

$array = Arrays::trim(Arrays::removeNull(Arrays::removeEmpty($array), true), true);
/**
 * [
 *     0 => 'first 1',
 *     3 => [
 *         0 => 'first 3',
 *         1 => false,
 *         'key2' => '',
 *         2 => '0',
 *         'key6' => 12,
 *         3 => [],
 *         4 => 'first 4'
 *     ],
 *     4 => true,
 *     'key8' => 'first 2 '
 * ]
 */
$array2 = Arrays::pullKey($array, 3);
/**
 * $array = [
 *     0 => 'first 1',
 *     4 => true,
 *     'key8' => 'first 2 '
 * ]
 * $array2 = [
 *     0 => 'first 3',
 *     1 => false,
 *     'key2' => '',
 *     2 => '0',
 *     'key6' => 12,
 *     3 => [],
 *     4 => 'first 4'
 * ]
 */
$array = Arrays::mergeNotEmptyData($array, $array2);
/**
 * [
 *     0 => 'first 3',
 *     4 => 'first 4',
 *     'key8' => 'first 2',
 *     'key6' => 12
 * ]
 */
Arrays::get($array, 0);
// 'first 3'

Introduction

CSE HELPERS is a collection of several libraries with simple functions written in PHP for people.

Despite using PHP as the main programming language for the Internet, its functions are not enough. ARRAY CSE HELPERS provides extra static methods allowing you to deal with arrays more efficiently.

CSE HELPERS was created for the rapid development of web applications.

CSE Helpers project:

Below you will find some information on how to init library and perform common commands.

Install

You can find the most recent version of this project here.

Composer

Execute the following command to get the latest version of the package:

composer require cse/helpers-arrays

Or file composer.json should include the following contents:

{
    "require": {
        "cse/helpers-arrays": "*"
    }
}

Git

Clone this repository locally:

git clone https://github.com/cs-eliseev/helpers-arrays.git

Download

Download the latest release here.

Usage

The class consists of static methods that are conveniently used in any project. See example examples-arrays.php.

GET array data by key

Example:

Arrays::get([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
], 'key1');
// value1

Set default value for not exist key:

Arrays::get([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
], 'key4', 'default1');
// default1

Pull array key

Example:

Arrays::pullKey([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
], 'key1');
// value1
/**
* [
*     'key2' => 'value2',
*     'key3' => 'value3'
* ]
*/

Set default value for not exist key:

Arrays::pullKey([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
], 'key4', 'default1');
// default1

Convert OBJECT TO ARRAY

Example:

$object = new \stdClass();
$object->key1 = 'value1';
$object->key2 = 'value2';
Arrays::objectToArray($object);
/**
* [
*     'key1' => 'value1',
*     'key2' => 'value2'
* ]
*/

Convert array TO html TAG

Example:

Arrays::toTags([
    'tag1' => [
        1,
        'attr1' => 1,
        'attr2' => 2
    ],
    'tag2' => 2,
    'tag3'
]);
// <tag1 attr1="1" attr2="2">1</tag1><tag2>2</tag2><tag3 />

Convert array to MAP

Data:

$array = [
    [
        'keyGroup1' => 'value1',
        'keyGroup2' => 'value2',
        'keyGroup3' => 'value3'
    ], [
        'keyGroup2' => 'value2',
        'keyGroup3' => 'value3'
    ], [
        'keyGroup1' => 'value1',
        'keyGroup3' => 'value2'
    ]
];

Example:

Arrays::map($array, 'keyGroup1');
/**
* [
*     'value1' => [
*           'keyGroup1' => 'value1',
*           'keyGroup3' => 'value2'
*     ]
* ]
*/

Change key group:

Arrays::map($array, 'keyGroup2');
/**
* [
*     'value2' => [
*           'keyGroup2' => 'value2',
*           'keyGroup3' => 'value3'
*     ]
* ]
*/

Set key value:

Arrays::map($array, 'keyGroup3', 'keyGroup1');
/**
* [
*     'value3' => null,
*     'value2' => 'value1'
* ]
*/

Array GROUP

Data:

$array = [
    [
        'keyGroup1' => 'value1',
        'keyGroup2' => 'value2',
        'keyGroup3' => 'value3'
    ], [
        'keyGroup2' => 'value2',
        'keyGroup3' => 'value3'
    ], [
        'keyGroup1' => 'value1',
        'keyGroup3' => 'value2'
    ]
];

Example:

Arrays::group($array, 'keyGroup1');
/**
* [
*     'value1' => [
*         [
*             'keyGroup1' => 'value1',
*             'keyGroup2' => 'value2',
*             'keyGroup3' => 'value3'
*         ], [
*             'keyGroup1' => 'value1',
*             'keyGroup3' => 'value2'
*         ]
*     ]
* ]
*/

Change key group:

Arrays::group($array, 'keyGroup2');
/**
* [
*     'value1' => [
*         [
*             'keyGroup1' => 'value1',
*             'keyGroup2' => 'value2',
*             'keyGroup3' => 'value3'
*         ], [
*             'keyGroup2' => 'value2',
*             'keyGroup3' => 'value3'
*         ]
*     ]
* ]
*/

Set key value:

Arrays::group($array, 'keyGroup3', 'keyGroup1');
/**
* [
*     'value3' => [
*         [
*             'value1',
*             null
*         ]
*     ],
*     'value2' => [
*         [
*             'value1'
*         ]
*     ],
* ]
*/

Array INDEX

Data:

$array = [
    [
        'keyGroup1' => 'value1',
        'keyGroup2' => 'value2',
        'keyGroup3' => 'value3'
    ], [
        'keyGroup2' => 'value2',
        'keyGroup3' => 'value3'
    ], [
        'keyGroup1' => 'value1',
        'keyGroup3' => 'value2'
    ]
];

Example:

Arrays::index($array, 'keyGroup1');
/**
* [
*     'value1' => [
*         [
*             'keyGroup1' => 'value1',
*             'keyGroup2' => 'value2',
*             'keyGroup3' => 'value3'
*         ], [
*             'keyGroup1' => 'value1',
*             'keyGroup3' => 'value2'
*         ]
*     ]
* ]
*/

Change key group:

Arrays::index($array, 'keyGroup2');
/**
* [
*     'value1' => [
*         [
*             'keyGroup1' => 'value1',
*             'keyGroup2' => 'value2',
*             'keyGroup3' => 'value3'
*         ], [
*             'keyGroup2' => 'value2',
*             'keyGroup3' => 'value3'
*         ]
*     ]
* ]
*/

Set key value:

Arrays::index($array, 'keyGroup3', 'keyGroup1');
/**
* [
*     'value3' => [
*         [
*             'value1',
*             null
*         ]
*     ],
*     'value2' => [
*         [
*             'value1'
*         ]
*     ],
* ]
*/

APPEND NOT EMPTY DATA

Example:

Arrays::appendNotEmptyData([
1 => 'first1',
    2 => '',
    3 => '0',
    4 => null,
    5 => 'first5',
    6 => 'first6',
    7 => 'first7',
    8 => [],
    9 => 'first9',
    11 => 'first11'
], [
    1 => 'second1',
    2 => 'second2',
    3 => 'second3',
    4 => 'second4',
    5 => '',
    6 => '0',
    7 => null,
    8 => 'second8',
    9 => [],
    10 => 'second10'
    12 => 0
]);
/**
* [
*     1 => 'first1',
*     2 => '',
*     3 => '0',
*     4 => null,
*     5 => 'first5',
*     6 => 'first6',
*     7 => 'first7',
*     8 => [],
*     9 => 'first9',
*     11 => 'first11',
*     10 => 'second10'
* ]
*/

REPLACE EMPTY NOT EMPTY DATA

Example:

Arrays::replaceEmptyNotEmptyData([
1 => 'first1',
    2 => '',
    3 => '0',
    4 => null,
    5 => 'first5',
    6 => 'first6',
    7 => 'first7',
    8 => [],
    9 => 'first9',
    11 => 'first11'
], [
    1 => 'second1',
    2 => 'second2',
    3 => 'second3',
    4 => 'second4',
    5 => '',
    6 => '0',
    7 => null,
    8 => 'second8',
    9 => [],
    10 => 'second10'
    12 => 0
]);
/**
* [
*     1 => 'first1',
*     2 => 'second2',
*     3 => 'second3',
*     4 => 'second4',
*     5 => 'first5',
*     6 => 'first6',
*     7 => 'first7',
*     8 => 'second8',
*     9 => 'first9',
*     11 => 'first11'
* ]
*/

REPLACE NOT EMPTY DATA

Example:

Arrays::replaceNotEmptyData([
1 => 'first1',
    2 => '',
    3 => '0',
    4 => null,
    5 => 'first5',
    6 => 'first6',
    7 => 'first7',
    8 => [],
    9 => 'first9',
    11 => 'first11'
], [
    1 => 'second1',
    2 => 'second2',
    3 => 'second3',
    4 => 'second4',
    5 => '',
    6 => '0',
    7 => null,
    8 => 'second8',
    9 => [],
    10 => 'second10'
    12 => 0
]);
/**
* [
*     1 => 'second1',
*     2 => 'second2',
*     3 => 'second3',
*     4 => 'second4',
*     5 => 'first5',
*     6 => 'first6',
*     7 => 'first7',
*     8 => 'second8',
*     9 => 'first9',
*     11 => 'first11'
* ]
*/

MERGE NOT EMPTY DATA

Example:

Arrays::mergeNotEmptyData([
1 => 'first1',
    2 => '',
    3 => '0',
    4 => null,
    5 => 'first5',
    6 => 'first6',
    7 => 'first7',
    8 => [],
    9 => 'first9',
    11 => 'first11'
], [
    1 => 'second1',
    2 => 'second2',
    3 => 'second3',
    4 => 'second4',
    5 => '',
    6 => '0',
    7 => null,
    8 => 'second8',
    9 => [],
    10 => 'second10'
    12 => 0
]);
/**
* [
*     1 => 'second1',
*     2 => 'second2',
*     3 => 'second3',
*     4 => 'second4',
*     5 => 'first5',
*     6 => 'first6',
*     7 => 'first7',
*     8 => 'second8',
*     9 => 'first9',
*     11 => 'first11',
*     10 => 'second10'
* ]
*/

Replace EMPTY array data TO NULL

Data:

$array = [
    0 => ' first 1 ', 
    'key1' => false, 
    1 => '', 
    2 => '0', 
    'key4' => null, 
    3 => [
        0 => ' first 3', 
        1 => false, 
        'key2' => '', 
        2 => '0', 
        'key5' => null, 
        'key6' => 12, 
        3 => [], 
        4 => 'first 4 '
    ], 
    4 => true, 
    5 => [], 
    'key8' => 'first 2 '
];

Example:

Arrays::emptyToNull($array);
/**
* [
*     0 => ' first 1 ', 
*     'key1' => null, 
*     1 => null, 
*     2 => null, 
*     'key4' => null, 
*     3 => [
*         0 => ' first 3', 
*         1 => false, 
*         'key2' => '', 
*         2 => '0', 
*         'key5' => null, 
*         'key6' => 12, 
*         3 => [], 
*         4 => 'first 4 '
*     ], 
*     4 => true, 
*     5 => null, 
*     'key8' => 'first 2 '
* ]
*/

Recursive:

Arrays::emptyToNull($array, true);
/**
* [
*     0 => ' first 1 ', 
*     'key1' => null, 
*     1 => null, 
*     2 => null, 
*     'key4' => null, 
*     3 => [
*         0 => ' first 3', 
*         1 => null, 
*         'key2' => null, 
*         2 => null, 
*         'key5' => null, 
*         'key6' => 12, 
*         3 => null, 
*         4 => 'first 4 '
*     ], 
*     4 => true, 
*     5 => null, 
*     'key8' => 'first 2 '
* ]
*/

REMOVE EMPTY data to array

Data:

$array = [
    0 => ' first 1 ', 
    'key1' => false, 
    1 => '', 
    2 => '0', 
    'key4' => null, 
    3 => [
        0 => ' first 3', 
        1 => false, 
        'key2' => '', 
        2 => '0', 
        'key5' => null, 
        'key6' => 12, 
        3 => [], 
        4 => 'first 4 '
    ], 
    4 => true, 
    5 => [], 
    'key8' => 'first 2 '
];

Example:

Arrays::removeEmpty($array);
/**
* [
*     0 => ' first 1 ', 
*     3 => [
*         0 => ' first 3', 
*         1 => false, 
*         'key2' => '', 
*         2 => '0', 
*         'key5' => null, 
*         'key6' => 12, 
*         3 => [], 
*         4 => 'first 4 '
*     ], 
*     4 => true, 
*     'key8' => 'first 2 '
* ]
*/

Recursive:

Arrays::removeEmpty($array, true);
/**
* [
*     0 => ' first 1 ', 
*     3 => [
*         0 => ' first 3', 
*         'key6' => 12, 
*         4 => 'first 4 '
*     ], 
*     4 => true, 
*     'key8' => 'first 2 '
* ]
*/

REMOVE NULL data to array

Data:

$array = [
    0 => ' first 1 ', 
    'key1' => false, 
    1 => '', 
    2 => '0', 
    'key4' => null, 
    3 => [
        0 => ' first 3', 
        1 => false, 
        'key2' => '', 
        2 => '0', 
        'key5' => null, 
        'key6' => 12, 
        3 => [], 
        4 => 'first 4 '
    ], 
    4 => true, 
    5 => [], 
    'key8' => 'first 2 '
];

Example:

Arrays::removeNull($array);
/**
* [
*     0 => ' first 1 ', 
*     'key1' => false, 
*     1 => '', 
*     2 => '0', 
*     3 => [
*         0 => ' first 3', 
*         1 => false, 
*         'key2' => '', 
*         2 => '0', 
*         'key5' => null, 
*         'key6' => 12, 
*         3 => [], 
*         4 => 'first 4 '
*     ], 
*     4 => true, 
*     5 => [], 
*     'key8' => 'first 2 '
* ]
*/

Recursive:

Arrays::removeNull($array, true);
/**
* [
*     0 => ' first 1 ', 
*     'key1' => false, 
*     1 => '', 
*     2 => '0', 
*     3 => [
*         0 => ' first 3', 
*         1 => false, 
*         'key2' => '', 
*         2 => '0', 
*         'key6' => 12, 
*         3 => [], 
*         4 => 'first 4 '
*     ], 
*     4 => true, 
*     5 => [], 
*     'key8' => 'first 2 '
* ]
*/

TRIM array data

Data:

$array = [
    0 => ' first 1 ', 
    'key1' => false, 
    1 => '', 
    2 => '0', 
    'key4' => null, 
    3 => [
        0 => ' first 3', 
        1 => false, 
        'key2' => '', 
        2 => '0', 
        'key5' => null, 
        'key6' => 12, 
        3 => [], 
        4 => 'first 4 '
    ], 
    4 => true, 
    5 => [], 
    'key8' => 'first 2 '
];

Example:

Arrays::trim($array);
/**
* [
*     0 => 'first 1', 
*     'key1' => false, 
*     1 => '', 
*     2 => '0', 
*     'key4' => null,
*     3 => [
*         0 => ' first 3', 
*         1 => false, 
*         'key2' => '', 
*         2 => '0', 
*         'key5' => null, 
*         'key6' => 12, 
*         3 => [], 
*         4 => 'first 4 '
*     ], 
*     4 => true, 
*     5 => [], 
*     'key8' => 'first 2'
* ]
*/

Recursive:

Arrays::trim($array, true);
/**
* [
*     0 => 'first 1', 
*     'key1' => false, 
*     1 => '', 
*     2 => '0', 
*     'key4' => null,
*     3 => [
*         0 => 'first 3', 
*         1 => false, 
*         'key2' => '', 
*         2 => '0', 
*         'key5' => null, 
*         'key6' => 12, 
*         3 => [], 
*         4 => 'first 4'
*     ], 
*     4 => true, 
*     5 => [], 
*     'key8' => 'first 2'
* ]
*/

Array TO STRING

Example:

Arrays::toString([
    'option1' => 'value1',
    'option2' => 'value2',
]);
// option1:value1;option2:value2;

Change delimiter:

Arrays::toString([
    'option1' => 'value1',
    'option2' => 'key1:value1;key1:value2;',
], '="', '" ');
// option1="value1" option2="key1:value1;key1:value2;" 

Testing & Code Coverage

PHPUnit is used for unit testing. Unit tests ensure that class and methods does exactly what it is meant to do.

General PHPUnit documentation can be found at https://phpunit.de/documentation.html.

To run the PHPUnit unit tests, execute:

phpunit PATH/TO/PROJECT/tests/

If you want code coverage reports, use the following:

phpunit --coverage-html ./report PATH/TO/PROJECT/tests/

Used PHPUnit default config:

phpunit --configuration PATH/TO/PROJECT/phpunit.xml

Donating

You can support this project here. You can also help out by contributing to the project, or reporting bugs. Even voicing your suggestions for features is great. Anything to help is much appreciated.

License

The ARRAYS CSE HELPERS is open-source PHP library licensed under the MIT license. Please see License File for more information.

GitHub @cs-eliseev