Array_walk_recursive - change both keys and values

How can you change both keys and values ​​with array_walk_recursive ??

Only values ​​are encoded here

 function _utf8_encode($arr){ array_walk_recursive($arr, 'utf8_enc'); return $arr; } function utf8_enc(&$value, &$key){ $value = utf8_encode($value); $key = utf8_encode($key); } 
+8
source share
3 answers

array_walk_recursive ONLY applies the user function to the VALUES values ​​of the array, not the indexes (I think this is because the array indices must be unique, so you cannot control them). It would be best to write a recursive function for yourself. The following should work:

 function utf8enc($array) { if (!is_array($array)) return; $helper = array(); foreach ($array as $key => $value) $helper[utf8_encode($key)] = is_array($value) ? utf8enc($value) : utf8_encode($value); return $helper; } $enc_array = utf8enc($your_array); 
+7
source

This is my recursive function, which can change not only the values ​​of an array like array_walk_recursive () , but also the keys of this array. It also preserves the order of the array.

 /** * Change values and keys in the given array recursively keeping the array order. * * @param array $_array The original array. * @param callable $_callback The callback function takes 2 parameters (key, value) * and returns an array [newKey, newValue] or null if nothing has been changed. * * @return void */ function modifyArrayRecursive(array &$_array, callable $_callback): void { $keys = \array_keys($_array); foreach ($keys as $keyIndex => $key) { $value = &$_array[$key]; if (\is_array($value)) { modifyArrayRecursive($value, $_callback); continue; } $newKey = $key; $newValue = $value; $newPair = $_callback ($key, $value); if ($newPair !== null) { [$newKey, $newValue] = $newPair; } $keys[$keyIndex] = $newKey; $_array[$key] = $newValue; } $_array = \array_combine($keys, $_array); } /** * Usage example */ modifyArrayRecursive($keyboardArr, function ($key, $value) { if ($value === 'some value') { return ['new_key_for_this_value', $value]; } return null; }); 
+1
source

Another recursive function in addition to the answer on the rabdde:

  function utf8_encode_array($array_to_encode=array()){ $encoded_array=array(); if(is_array($array_to_encode)){ foreach($array_to_encode as $key => $value){ $key=utf8_encode($key); if(is_array($value)){ $encoded_array[$key]=utf8_encode_array($value); } else{ $encoded_array[$key]=utf8_encode($value); } } } return $encoded_array; } 
0
source

Source: https://habr.com/ru/post/897708/


All Articles