jongpak/array-key-glue

Implode keys of array recursively

dev-master 2016-09-11 15:41 UTC

This package is not auto-updated.

Last update: 2024-11-23 20:27:32 UTC


README

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