1

I have a multidimensional array like this (please ignore the strlen):

array(2) {
  ["document"]=>
    array(16) {
      [0]=>
      string(14) "Value1"
      [1]=>
      string(4) "Value2"
      [2]=>
      string(30) "Value3"
  ...

And I want to call "strtoupper" for every element (Value1, Value2, etc.) on each level of the multidimensional array (document, etc.).

I tried array_walk_recursive($array, "strtoupper"); but it doesn't work. But why, what can I do?

2
  • Note that array_walk_recursive only visits leafs. If you want 'document', which is not a leaf, also to be uppercase, you will need a different approach. Commented May 14, 2022 at 19:50
  • Thank you, interesting! I only need leafs but I read your solution and will keep that in mind. Commented May 14, 2022 at 20:55

3 Answers 3

2

As strtoupper does not change the original value, but returns new value instead, you should do this:

array_walk_recursive(
    $array, 
    // pass value by reference. 
    // Changing it will also reflect changes in original array
    function (&$value) { $value = strtoupper($value); } 
);

Simple fiddle.

Also this hint is described in manual, see the note on callback parameter description.

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

1 Comment

Thank you!! Found the "note" in the manual. I didn't read it as I am not that used to using the manual already. Will do that from now on. Thank you! My thinking was that as array_walk_recursive has not an array as return value, it's designed to change the array directly. But that was wrong apparently.
1
function recurse(array $array): array {
  $result = [];
  foreach ($array as $key => $value) {
    $newKey = is_string($key) ? strtoupper($key) : $key;
    if (is_array($value)) {
      $result[$newKey] = recurse($value);
    } elseif (is_string($value)) {
      $result[$newKey] = strtoupper($value);
    } else {
      $result[$newKey] = $value;
    }
  }
  return $result;
}

$array = [
    'document'  => [
        'Value1',
        'Value2',
        'Value3'
    ],
    'document2' => [
        'Value21',
        'Value22',
        'document2' => [ 'Value221', 222, 'Value223' ],
        23
    ]
];

$result = recurse($array);

print_r($result);

Comments

0
array_walk_recursive($array, static fn (&$val) => $val = $callback($val));

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

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.