/array-key-glue

Implode keys of array recursively

Primary LanguagePHPMIT LicenseMIT

jongpak/array-key-glue

Implode keys of array recursively

Build Status Scrutinizer Code Quality codecov

Simple usage

use Prob\ArrayUtil\KeyGlue;

$array = [
    'A' => [
        'B' => 'Value1',
        'C' => 'Value2',
        'D' => [
            'E' => 'Value3',
            'F' => 'Value4'
        ]
    ],
    'G' => [
        'H',
        'I',
        'J'
    ]
];

$glue = new KeyGlue();
$glue->setArray($array);
$glue->setGlueCharacter('.');   // glue '.'
$glue->glueOnlyKey();
/*
 * Array
 * (
 *     [0] => A.B
 *     [1] => A.C
 *     [2] => A.D.E
 *     [3] => A.D.F
 *     [4] => G
 * )
 */
$glue->setGlueCharacter('.');   // glue '.'
$glue->glueKeyAndContainValue();
/*
 * Array
 * (
 *     [A.B]    => 'Value1'
 *     [A.C]    => 'Value2'
 *     [A.D.E]  => 'Value3'
 *     [A.D.F]  => 'Value4'
 *     [G]      => ['H', 'I', 'J']
 * )
 */
$glue->setGlueCharacter(' -> ');    // glue ' -> '
$glue->glueOnlyKey();
/*
 * Array
 * (
 *     [0] => A -> B
 *     [1] => A -> C
 *     [2] => A -> D -> E
 *     [3] => A -> D -> F
 *     [4] => G
 * )
 */
$glue->setGlueCharacter(' -> ');    // glue ' -> '
$glue->glueKeyAndContainValue();
/*
 * Array
 * (
 *     [A -> B]         => 'Value1'
 *     [A -> C]         => 'Value2'
 *     [A -> D -> E]    => 'Value3'
 *     [A -> D -> F]    => 'Value4'
 *     [G]              => ['H', 'I', 'J']
 * )
 */