我想在不破坏结构的情况下清理我的阵列。所以我想删除每个元素的值。问题是,数组有更多的维度。
[array]
[id] = "llll"
[innerArray]
[name] = "namenamename"
[name] = "kdkdfk"
[arryme]
[id] = 121212
[arraytarra] = "dd"
应该
[array]
[id] = ""
[innerArray]
[name] = ""
[name] = ""
[arryme]
[id] = 0
[arraytarra] = ""
这是我的开始:
private function cleanArray($array) {
$return = $array;
foreach($array as $key => $value) {
if(is_Array($array[$key])) $this->cleanArray($array[$key]);
else $return[$key] = "";
}
}
最终版本,感谢 bwoebi:
/****************
* cleanArray()
****************
* cleans an array: deletes the values but let the keys
*/
private function cleanArray($array) {
foreach($array as &$value) {
if(is_Array($value)) $value = $this->cleanArray($value);
else if(is_Int($value)) $value = 0;
else if(is_Bool($value)) $value = false;
else $value = "";
}
return $array;
}