array_walk_recursive - 修改键和值

cla*_*rkk 8 php

如何用array_walk_recursive?? 修改键和值?

这里只对值进行编码

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);
}
Run Code Online (Sandbox Code Playgroud)

rab*_*dde 7

array_walk_recursive只在数组的VALUES上应用用户函数,而不是在索引上应用(我认为它有一些事实,即数组的索引必须是唯一的,所以你不能操纵它们).最好的办法就是自己写一个递归函数.以下应该有效:

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);
Run Code Online (Sandbox Code Playgroud)