Php - Foreach变量

问题描述 投票:3回答:1

我想为foreach循环中的值赋值我为此定义了一个变量,但是当我打印时它返回一个空值

$authority = getWithResult('authorites', 'UserId', $id);
$sidebar = null;
foreach($authority as $yetki){
    $sidebar = getWithResult('sidebar', 'Id', $yetki->SidebarId);
}
print_r($sidebar);

输出:数组()

php codeigniter
1个回答
4
投票

使用下面的代码,在你的情况下$ sidebar值总是覆盖

$authority = getWithResult('authorites', 'UserId', $id);
$sidebar = [];
foreach($authority as $yetki){
    $sidebar[] = getWithResult('sidebar', 'Id', $yetki->SidebarId);
}
print_r($sidebar);
© www.soinside.com 2019 - 2024. All rights reserved.