Raz*_*mun 2 php parameters codeigniter array-walk
我有一个变量$id = 10
,需要在里面使用array_walk()
.如下:
$id = 10;
array_walk($profile_items, function(&$a) {
$count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id', $id)->count_all_results('cover_type_profile_items_link');
$a['selected'] = $id;
});
echo "<pre>";
print_r($profile_items).exit;
Run Code Online (Sandbox Code Playgroud)
当我在其中使用$id
变量时array_walk()
显示错误.
消息:未定义的变量:id
有解决方案吗
谢谢你的建议
您可以使用use
关键字:
array_walk($profile_items, function(&$a) use($id) {
Run Code Online (Sandbox Code Playgroud)
所以,
$id = 10;
array_walk($profile_items, function(&$a) use($id) {
$count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id', $id)->count_all_results('cover_type_profile_items_link');
$a['selected'] = $id;
});
echo "<pre>";
print_r($profile_items);
Run Code Online (Sandbox Code Playgroud)
要通过引用继承,请添加&符号:
array_walk($profile_items, function(&$a) use(&$id) {
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1120 次 |
最近记录: |