Php - Foreach变量

I want to assign a value to the values ​​in the foreach loop I've defined a variable for this, but it returns a null value when I print

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

output: Array ( )

Use below code, In your case $sidebar value always override

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