0

I have a multidimensional array that may or may not contain the key name one or more times. What I'd like to do is for every instance of said element insert another element, next to that element, with the key key.

So, given this array:

[
    [
        'noname' => 'No name',
        'label' => 'I have no name'
    ],
    [
        'name' => 'foo',
        'label' => 'Foo',
        'fields' => [
            [
                'name' => 'bar',
                'label' => 'Bar'
            ]
        ]
    ],
    [
        'name' => 'baz',
        'label' => 'Baz'
    ]
]

I'd like the following output:

[
    [
        'noname' => 'No name',
        'label' => 'I have no name'
    ],
    [
        'name' => 'foo',
        'key' => 'foo-key', # This is inserted by the function
        'label' => 'Foo',
        'fields' => [
            [
                'name' => 'bar',
                'key' => 'bar-key', # This is inserted by the function
                'label' => 'Bar'
            ]
        ]
    ],
    [
        'name' => 'baz',
        'key' => 'baz-key', # This is inserted by the function
        'label' => 'Baz'
    ]
]

I've looked into array_walk_recursive but can't get it to work. Do I need to write my own recursive function or is there something appropriate built in that I can use for this?

3
  • Share the code you wrote using array_walk_recursive & what didn't work. Commented Feb 8, 2017 at 18:54
  • Tbh I got stumped immediately because I couldn't seem to access the original array from within my array_walk_recursive callback... I'm pretty sure I could write a custom function for this, but was still hoping for some suggestions on built in functions or other ideas I may have overlooked. Commented Feb 8, 2017 at 18:59
  • 1
    array_walk_recursive gives you the elements that are not array, if it encounters any array element, it keeps digging down. That's probably why it didn't work for you. Anyway, if you use the 'use' keyword to send your array into the closure scope, you can access it (still wont help you with this case). Commented Feb 8, 2017 at 19:12

1 Answer 1

2

Your code would be something like this:

<?php
$array = [
    [
        'noname' => 'No name',
        'label' => 'I have no name'
    ],
    [
        'name' => 'foo',
        'label' => 'Foo',
        'fields' => [
            [
                'name' => 'bar',
                'label' => 'Bar'
            ]
        ]
    ],
    [
        'name' => 'baz',
        'label' => 'Baz'
    ]
];

function fix_array($array){
    foreach ($array as $key => $value){
        if (is_array($value)){
            $array[$key] = fix_array($value);
        }
        elseif ($key == 'name'){
            $array['key'] = $value . '-key'; 
        }
    }
    return $array;
}

$new_array = fix_array($array);

print_r($new_array);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, I guess it doesn't get much simpler than that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.