Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php-Foreach变量_Php_Codeigniter - Fatal编程技术网

Php-Foreach变量

Php-Foreach变量,php,codeigniter,Php,Codeigniter,我想为这些值指定一个值​​在foreach循环中,我为此定义了一个变量,但在打印时它返回一个空值 $authority=getWithResult('authorites','UserId',$id); $sidebar=null; foreach($yetki的权限){ $sidebar=getWithResult('sidebar','Id',$yetki->SidebarId); } 打印(侧边栏); 输出: 数组()使用下面的代码,在您的情况下,$sidebar值始终覆盖 $autho

我想为这些值指定一个值​​在foreach循环中,我为此定义了一个变量,但在打印时它返回一个空值

$authority=getWithResult('authorites','UserId',$id);
$sidebar=null;
foreach($yetki的权限){
$sidebar=getWithResult('sidebar','Id',$yetki->SidebarId);
}
打印(侧边栏);
输出:
数组()

使用下面的代码,在您的情况下,$sidebar值始终覆盖

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

您确定
$authority
不是空的吗?如果对
getWithResult
的最后一次调用返回一个空数组,则代码仅按所述工作。你可能想试试@soyab badi发布的代码,看看到底发生了什么。