1

I have a a Multidimensional array with a lot of content that includes a label for each piece of content, and I have a separate array with a Key that is the label being searched for and the value that is to replace it.

I want to use the second array to replace nested label values in the first array. The function should find the first Array's label value based on the key of the second array, and replace it using that Key's value.

For Example the content Array:

Array
(
    [Name] => Array
        (
            [0] => Array
                (
                    [label] => Name
                    [content] => Important Paper
                )

            [1] => Array
                (
                    [label] => Item Type
                    [content] => Document
                )

            [2] => Array
                (
                    [label] => Author
                    [content] => Bob Jones
                )
        )

    [date] => Array
        (
            [0] => Array
                (
                    [label] => Date
                    [content] => 2009
                )
        )
)

And an example of the array looking to replace the value

Array
(
    [Name] => Record
    [Author] => Researcher
    [Date] => Year
)

The output I would want from this function would resemble

Array
(
    [Name] => Array
        (
            [0] => Array
                (
                    [label] => Record
                    [content] => Important Paper
                )

            [1] => Array
                (
                    [label] => Item Type
                    [content] => Document
                )

            [2] => Array
                (
                    [label] => Researcher
                    [content] => Bob Jones
                )
        )

    [date] => Array
        (
            [0] => Array
                (
                    [label] => Year
                    [content] => 2009
                )
        )
)

Right now I am attempting to get the results by using a series of forloops and if statements. I don't actually know the depth that the [label] key will be found at, and it should only update content with the key value [label].

Any insight on a way to do this more efficiently would be great. I've also been looking at all of the different php array functions and trying to find the best combinations.

0

1 Answer 1

1

you can use: array_walk_recursive

from: php.net

$orig = array('Name' => [['label' => 'Name', 'content' => 'Important Paper'],
                         ['label' => 'Item Type', 'content' => 'Document'],
                         ['label' => 'Author', 'content' => 'Bob Jones']],
              'date' => [[['label' => 'Date', 'content' => '2009']]]);

// notice the `&` symbol, this allows us to change the original array on the fly
function replaceIt(&$item){
    $replaceArray = ['Name' => 'Record',
                     'Author' => 'Researcher',
                     'Date' => 'Year'];

    foreach ($replaceArray as $key => $value) {
        if($key == $item){ $item = $value; }
    }
}

array_walk_recursive($orig, 'replaceIt');

print('<pre>');
print_r($orig);
print('</pre>');
Sign up to request clarification or add additional context in comments.

2 Comments

you only care about the leaves plain values anyway i thought? you are simply checking each leaf to see if its a certain value then replacing? given your example my solution works, update your example array with more details.
Sorry, was hung up on passing array values in through the another function, I called the function directly in the array_walk_recursive rather than the function name and called the replace array in that function. Thanks!

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.