array_walk array_map array_filter
array_map
$array = [
0=>['sku_id'=>'11','sku_amount'=>240],
1=>['sku_id'=>'27','sku_amount'=>600]
];
$packSkuNum=[];
array_map(function($item)use(&$packSkuNum){
$packSkuNum[$item['sku_id']]=$item['sku_amount'];
},$array);
print_r($packSkuNum);
<?php
$a = [];
$b = ["Horse","Dog","Cat"];
array_map(function ($value) use (&$a){
if($value==="Dog"){$a[] = $value;}},
$b);
print_r($a);
$a1 = ["Horse","Dog","Cat"];
$a2 = ["Cow","Dog","Rat"];
$a3 = [];
array_map(function ($v1,$v2) use (&$a3) {
if($v1==$v2){
//unset($v1);
$a3[] = $v1;
//var_dump($v1);
}
},$a1,$a2);
var_dump($a3);
array_walk($a = ["a"=>"red","b"=>"green","c"=>"blue"], function ($value,$key){
echo $key.'------'.$value;
});
$a2 = ["Cow","Dog","Rat","Dog","Pig"];
$arr = [];
$new_arr = array_map(function ($item) use (&$arr) {
if(!in_array($item,$arr)){
$arr[] = $item;
return $item;
}
return false;
},$a2);
var_dump( $new_arr);
使用“&”取址符就可以赋值闭包外的变量了。
array_walk
$info = $this->getPaService()->recordInfo($data);
$prekey = RecCoant::REC_QA_URL . $data['vod_id'];
array_walk($info['video_info'], function (&$value) use ($prekey) {
$value['down_url'] = $this->getRedis()->get($prekey . ':' . $value['quality']);
});
return $info;
array_filter()、array_map()、array_walk()三者的用法和区别
PHP 使用 array_map 替代 foreach
PHP array_walk_recursive 和 array_map 用法及区别 以及在其中使用闭包(匿名函数)use()
array_map常用技巧