例如,如何转换以下数组:
$array1 = array("value1" => "20", "value2" => 40, array("value3" => 60));
要:
$array1 = array("value1" => "20", "value2" => "40", array("value3" => "60"));
答案 0 :(得分:7)
array_walk_recursive($array, function (&$value) { $value = (string)$value; });
答案 1 :(得分:0)
您可以编写递归函数,例如:
function strArr($inp){
$res=array();
foreach($inp as $k=>$v){
if(is_array($v)) $res[$k]=strArr($v);
else $res[$k]=strval($v);
}
return $res;
}
$array1 = array("value1" => "20", "value2" => 40, array("value3" => 60));
$array2 = strArr($array1);
答案 2 :(得分:-1)
在不支持lambda函数的情况下将每个元素转换为字符串:
array_walk_recursive($array, 'strval');