array_walk_recursive()
遍歷數(shù)組篮奄,把數(shù)組元素的鍵值和鍵名,傳遞給給回調(diào)函數(shù)處理,返回 boolean
同 arra_walk()的區(qū)別是颂碧,array_walk_recursive 會(huì)遞歸到數(shù)組的更深層次;遍歷的順序是從左到右按照書寫的順序,遞歸處理完第一個(gè)元素后类浪,在一次處理后面的元素载城;
基礎(chǔ)語法:
array_walk_recursive(array $array , callback $callback [, mixed $usedata = NULL]);
array $array ——被遍歷的數(shù)組;
callback $callback ——接受數(shù)組元素的回調(diào)函數(shù)费就,第一個(gè)參數(shù)是 鍵值诉瓦,第二個(gè)參數(shù)是鍵名,第三個(gè)參數(shù)是mixed $usedata;
mixed $usedata ——傳遞給函數(shù)的第三個(gè)參數(shù)力细;
語法結(jié)構(gòu)1:
array_walk_recursive(array $array ,callback $callback)
沒有多余數(shù)據(jù)參與的遞歸遍歷睬澡;
實(shí)例:
$a_personal_info = [
'bill'=>array ('name'=>'bill','job'=>'worker','age'=>33),
'thomas'=>array ('name'=>'thomas','job'=>'driver','age'=>23)
];
function print_age ($v,$k){
if($k == 'name'){
echo $v.'的年紀(jì)是';
}else if($k == 'age'){
echo $v.'!<br/>';
}
}
array_walk_recursive($a_personal_info,'print_age');
語法結(jié)構(gòu)2:
array_walk_recursive(array $array , callback $callback )
引用傳遞修改鍵值;
實(shí)例:
$a_employee = [
'bill'=>array ('name'=>'bill','job'=>'worker','age'=>33,'salar'=>3000),
'thomas'=>array ('name'=>'thomas','job'=>'driver','age'=>23,'salar'=>2000)
];
function change_salar(&$v,$k){
if($k == 'salar'){
$v *= 1.08;
}
}
array_walk_recursive($a_employee,'change_salar');
echo $a_employee['bill']['salar']
.'<br/>'
.$a_employess['thomas']['salar'];
#3240
#2160
語法結(jié)構(gòu)3:
array_walk_recursive(array $array ,callback $callback,mixed $usedata)
傳遞另一個(gè)參數(shù)給回調(diào)函數(shù)眠蚂,參與計(jì)算煞聪;
實(shí)例:
$a_employee = [
'bill'=>array ('name'=>'bill','job'=>'worker','age'=>33,'salar'=>3000,'diredate'=>2007),
'thomas'=>array ('name'=>'thomas','job'=>'driver','age'=>23,'salar'=>2000,'dirdate'=>2016)
];
function change_salar(&$v,$k,$ratio){
if($k == 'salar'){
$v *=$ratio
}
}
array_walk_recursive($a_employee,'change_salar',1.08);
echo $a_employee['bill']['salar']
.'<br/>'
.$a_employee['thomas']['salar'];
#3240
#2160
array_walk_recursive() 可以遞歸遍歷
array_walk() 不可以遞歸遍歷;
主要的區(qū)別就是逝慧,是否對(duì)多維數(shù)組進(jìn)行遞歸遍歷昔脯;