Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php recursive key renaming for inner arrays only

Tags:

i am in need of bit direction here for renaming my inner child keys who inner depth is unknown. Original array is parent child multidimensional array created via id and parent_id relation. Here's a one example of array


$testArray = array (
    "name" => "Test name",
    "someValue1" => 834.69,
    "someMoreValue" => 33.4,
    "someCode" => 6668,
    "child" => array
        (
            array
                (
                    "name" => "Some name",
                    "someValue1" => 471.05,
                    "someMoreValue" => 18.84,
                    "someCode" => 7064,
                    "child" => array
                        (
                            array
                                (
                                    "name" => "Yet another name",
                                    "someValue1" => 438.62,
                                    "someMoreValue" => 17.56,
                                    "someCode" => 7065
                                ),
                            array
                                (
                                    "name" => "Da name",
                                    "someValue1" => 4444,
                                    "someMoreValue" => 44,
                                    "someCode" => 7044
                                )
                        )
                ),
                array
                    (
                        "name" => "name",
                        "someValue1" => 2222,
                        "someMoreValue" => 22,
                        "someCode" => 7022
                    )
        )
);

here's my trying so far, i am able to rename the first level of childs only.

function keyprefix($keyprefix, $keyprefix2, Array $array) {
    foreach($array as $k=>$v){
        $array[$keyprefix.'-'.$k.'-'.$keyprefix2] = $v;
        unset($array[$k]);
    }
    return $array; 
}
function test($array) {
    $newArr = array();
    foreach ($array as $key => $value) {
        // $newArr[] = is_array($value) ? test($value) : $value;
        // $newArr[] = is_array($value) ? $array : array_merge( keyprefix("$name","Alt Danışman", $array[$key]) );
        $index = 0;
        $name = $array['name'];
        if (is_array($value)) {
            //if (is_array($value)) {
            //    test($value);
            //}
            $newArr[$key] = array_merge(
                keyprefix("$name","Under Child", $array[$key])
            );
        } 
        else {
            $newArr[$key] = $value;
        }
    }
    return $newArr;
}

echo highlight_string(var_export(test($testArray), true));

enter image description here

Any ideas on how I can approach this? I feel that I am close just need a little guide in the right path. I have seen many examples of stack-overflow etc but need some help. I know recursion is not my best skill yet.

like image 617
Danish Avatar asked Feb 11 '19 02:02

Danish


1 Answers

function TryRenameChildren(array $array)
{
    if(isset($array['child']))
    {
        $array['child'] = keyprefix($array['name'], 'prefix2', $array['child']);

        foreach($array['child'] as $key => $value)
            if(is_array($value))
                $array['child'][$key] = TryRenameChildren($value);
    }

    return $array;
}

$testArray = TryRenameChildren($testArray);
like image 136
shingo Avatar answered Oct 11 '22 20:10

shingo