$arr = [1, 2, 3, 4, 5];
// 遍历数组,执行回调
$res = array_map(function ($num) {
return $num * $num;
}, $arr);
var_dump($res);
例二 传参
$arr = [1, 2, 3, 4, 5];
$a=123;//传入参数
$num=321;//参数
// 遍历数组,执行回调
$res = array_map(function ($num) use ($a) {
return $num * $num;
}, $arr);
var_dump($res);
实战
public function getMemberList(array $params1)
{
$countSqlObj = User::select();
$page = $params1['page'];
$page_size = $params1['limit'];
$countSqlObj->orderBy('id','desc');
if (isset($params['keyword'])) {
$countSqlObj->where('nickname', 'like', "%{$params1['keyword']}%");
}
$count = $countSqlObj->count();
$rows = $countSqlObj->forPage($page, $page_size)->get()->toArray();
$runIdAll = ServerRunID::getInstance()->getServerRunIdAll();
return $this->getPagingRows(
array_map(function ($item) use ($runIdAll) {
$item['is_online'] = di()->get(SocketClientService::class)->isOnlineAll($item['id'], $runIdAll);
return $item;
}, $rows)
, $count, $page, $page_size);
}
array_map — 为数组的每个元素应用回调函数
最新推荐文章于 2024-09-11 06:55:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)