douquanzhan0315 2019-03-08 09:35
浏览 35
已采纳

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 ( )

  • 写回答

1条回答 默认 最新

  • douxuanyi2813 2019-03-08 09:41
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?