3

I have an array like this:

$a = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => array(
        'key4' => 'value4',
        'key5' => array(
            'key6' => 'value6'
        )
    )
);

as you can see there are inner arrays inside $a

Now, I have a list of keys, example:

key1
key4
key6

I need a script that search if those key exists, and if exists change their values. I Need to change their values with base64_encode($value_of_the_key)

so Maybe a callback that get the current value and convert it using base64_encode() function.

COuld someone help me?

I'm tring to see the current php functions but it seems there is not ones that do this thing.

THanks

EDIT:

Using the follow code i can get the keys in the callback....but the problem is:

How can i modify the values directly in the array? I Mean.... ok ... i get key and value, but how to change the value in the original array? ($a)

$a = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => array(
        'key4' => 'value4',
        'key5' => array(
            'key6' => 'value6'
        )
    )
);


function test($item, $key)
{
    echo "$key. $item<br />\n";
}


array_walk_recursive($a, 'test');
0

1 Answer 1

5

array_walk_recursive() with callback supplied should help. More info here.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the reply, but please take a look at the edit i made in the question. Thanks
@Dail use function test(&$item, $key) (note the &) and modify $item inside the function.

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.