PHP code example of cse / helpers-arrays

1. Go to this page and download the library: Download cse/helpers-arrays library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cse / helpers-arrays example snippets


$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'

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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'
* ]
*/

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'
* ]
*/

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'
* ]
*/

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'
* ]
*/

$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 '
];

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 '
* ]
*/

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 '
* ]
*/

$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 '
];

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 '
* ]
*/

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

$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 '
];

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 '
* ]
*/

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 '
* ]
*/

$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 '
];

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'
* ]
*/

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'
* ]
*/

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

Arrays::toString([
    'option1' => 'value1',
    'option2' => 'key1:value1;key1:value2;',
], '="', '" ');
// option1="value1" option2="key1:value1;key1:value2;" 
bash
phpunit PATH/TO/PROJECT/tests/
bash
phpunit --configuration PATH/TO/PROJECT/phpunit.xml