下午看oc代码,看到这么一句
array_multisort($sort_order, SORT_ASC, $method_data);
$arr = array(10, 102, 199, "a"=>array('B','c','d'=>array('e','f')),'g','h'); 想递归,想判断数组深度。
然后半成品是这么个结果:
function array_depth($array) {
$max_depth = 1;
foreach ($array as $value) {
if (is_array($value)) {
$depth = array_depth($value) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}
array_depth($ar);
function toto($arr,$depth='1'){
$tem=array();
foreach ($arr as $key => $value) {
if(is_string($value)){
$tem[]=strtoupper($value);
}elseif(is_array($value)&&array_depth($value)>1){
// $J=str_repeat('[]', (array_depth($value)));
$tem[]=array_map('strtoupper',$value);
}else{
$tem[]=$value;
}
}
return $tem;
}$arr1[][][]=5;
$j=str_repeat('[]', 3);
怎么办,去牛逼传说的stackoverflow,我也溢出一下。
老外的人真牛逼,我写问题用了3分钟,等待有人回答用了10秒。
第一个朋友说用一个函数就行,不需要知道什么数组深度。那么函数就是array_walk_recursive,之前只是简单用过array_map,array_map不能递归,所以他推荐用这个。
我试了一下,还是能达到部分效果,够用了,代码如下
$arr = array(10, 102, 199, "a"=>array('B','c','d'=>array('e','f')),'g','h');
array_walk_recursive($arr, function(&$value,$key){
$value= strtoupper($value);
});
var_dump($arr);然后第一个回答我的人说,变不了key的值。
然后第二个人就咔嚓,show he the code
然后就有了以下的牛逼答案,亮瞎了。
$arr = array(10, 102, 199, "a"=>array('B','c','d'=>array('e','f')),'g','h');
$arr = json_decode(strtoupper(json_encode($arr)),true);
var_dump($arr);
die;还是需要把php数组函数搞透彻再说。
ps:不要担心自己英语low,会有人矫正。。大神啊。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/tstbdt/article/details/46686475