Home > database >  PHP - Recursively insert a key/value pair after a specific key in multidimensional array
PHP - Recursively insert a key/value pair after a specific key in multidimensional array

Time:11-20

I have an indexed array containing any number of nested associative or indexed arrays. I need to recursively insert a new key/value pair 'apple' inside each array if it contains a key 'banana'.

$original = [
    0 => [
        'something1' => 'something',
        'banana' => 'yellow',
        'something2' => [
            'something3' => 'something',
            'something4' => [
                'something5' => 'something',
                'banana' => 'yellow',
                'something6' => [
                    'banana' => 'yellow',
                ]
            ],
            'banana' => 'yellow',
        ],
        'something7' => [
            0 => [
                'something8' => 'something',
                'banana' => 'yellow',
            ],
        ],
    ],
    1 => [
        'something9' => 'something',
        'banana' => 'yellow',
    ],
    2 => [
        'something10' => 'something',
    ],
];

This is the expected result - 'apple' doesn't have to be inserted immediately next to 'banana', it can be located anywhere in the same level array as long as it's a sibling of 'banana'.

$new = [
    0 => [
        'something1' => 'something',
        'banana' => 'yellow',
        'apple' => 'red',
        'something2' => [
            'something3' => 'something',
            'something4' => [
                'something5' => 'something',
                'banana' => 'yellow',
                'apple' => 'red',
                'something6' => [
                    'banana' => 'yellow',
                    'apple' => 'red',
                ]
            ],
            'banana' => 'yellow',
            'apple' => 'red',
        ],
        'something7' => [
            0 => [
                'something8' => 'something',
                'banana' => 'yellow',
                'apple' => 'red',
            ],
        ],
    ],
    1 => [
        'something9' => 'something',
        'banana' => 'yellow',
        'apple' => 'red',
    ],
    2 => [
        'something10' => 'something',
    ],
];

I could find answers on how to insert key/value pair after certain key in a flat array, but not an answer for doing so recursively in a multidimensional array.

CodePudding user response:

    $original = [
    0 => [
        'something1' => 'something',
        'banana' => 'yellow',
        'something2' => [
            'something1' => 'something',
            'something2' => [
                'something1' => 'something',
                'banana' => 'yellow',
                'something2' => [
                    'banana' => 'yellow',
                ]
            ],
            'banana' => 'yellow',
        ],
        'something3' => [
            0 => [
                'something' => 'something',
                'banana' => 'yellow',
            ],
        ],
    ],
    1 => [
        'something' => 'something',
        'banana2' => 'yellow',
    ],
];

function ins_value_to_arr(&$arr, $insVal = [] , $sibling = NULL){
    static  $ins; 
    static $sib;
    if($insVal){
        $ins = $insVal;
        $sib = $sibling;
    }
    $merge = false;
    foreach($arr as $k => &$val){
        if(is_array($val)){
           ins_value_to_arr($val); 
        }
        if($k == $sib){
            $merge = true;
        }
    }
    if($merge){
        $arr = array_merge($arr,$ins);
    }
    
    return $arr;
}

// call -> ins_value_to_arr($original,['apple' => 'red'],'banana');
  • Related