Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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_Mysql - Fatal编程技术网

Php 在数组中使用foreach

Php 在数组中使用foreach,php,mysql,Php,Mysql,我目前正在尝试生成一个动态子导航。因此,我从数据库中提取数据,并将其中的一些数据存储到一个数组中。现在我想在这个数组中做一个foreach。据我所知,这是不可能的 但也许我错了。我想知道这是否可能,如果可能,我该怎么做 这是我的代码,它不会工作,因为它会产生语法错误 $this->subnav = array( '' => array( 'Test Link' =>

我目前正在尝试生成一个动态子导航。因此,我从数据库中提取数据,并将其中的一些数据存储到一个数组中。现在我想在这个数组中做一个foreach。据我所知,这是不可能的

但也许我错了。我想知道这是否可能,如果可能,我该怎么做

这是我的代码,它不会工作,因为它会产生语法错误

$this->subnav = array(
                        '' => array(
                            'Test Link'     => 'login.php',
                            'Badged Link'   => array('warning',10023,'check.php')
                        ),
                        'benutzer'      => array(
                            'Benutzer suchen'       => '/mother/index.php?page=benutzer&subpage=serach_user',
                            'Benutzer hinzufügen'   => '/mother/index.php?page=benutzer&subpage=add_user',
                            'Rechtevergabe'         => '/mother/index.php?page=benutzer&subpage=user_rights'
                        ),
                        'logout'        => array(
                            'Login'     => '/mother/login.php',
                            'Logout'    => '/mother/index.php?page=logout'
                        ),
                        'datenbank'        => array(
                             (foreach($this->system->get_databases() as $db){array($db->name => $db->url)}),
                            'Deutschland'           => '/mother/login.php',
                            'Polen'                 => '/mother/index.php',
                            'Spanien'               => '/mother/index.php',
                            'Datenbank hinzufügen'  => '/mother/index.php?page=datenbank&subpage=add_database'
                        )
                    );
}

不能将foreach循环放在这样的数组中。不过你可以这样做

foreach($this->system->get_databases() as $db)
{
    $this->subnav['datenbank'][$db->name] = $db->url;
}

这是不可能的。但是你可以用另一种方式,比如你可以把foreach outsite和这个数组的顶部,分配给一个数组,然后你可以使用这个数组变量

e、 g

现在将其分配给:

'datenbank'        => $arrDB

可能有助于将您的
foreach
放在数组上方,让它将数据库中的值分配给变量,然后将变量传递到arrayThx。我希望这会成为可能,但我想我必须坚持这一点:)
'datenbank'        => $arrDB