ThatMSG
ThatMSG

Reputation: 1506

Use foreach in an array

im currently trying to generate a dynamic subnavigation. Therefor i pull data off of the database and store some of it an an array. Now i want to do an foreach in this array. Well as far as I know, this isn't possible.

But maybe I'm wrong. I would like to know if it would be possible, and if so how do i do it?

This is my code, which wont work since it hands out an syntax error.

$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'
                        )
                    );
}

Upvotes: 0

Views: 81

Answers (2)

skparwal
skparwal

Reputation: 1084

This is not possible. but you can do it in other way like you can place the foreach outsite and top of this array and assign to an array and then you can use that array variable.

e.g.

$arrDB = array();
foreach($this->system->get_databases() as $db) { 
  $arrDB[$db->name] = $db->url;
}

Now assign it to:

'datenbank'        => $arrDB

Upvotes: 0

Ali R.
Ali R.

Reputation: 28

You can't place foreach loop inside an array like this. You can do something like this though.

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

Upvotes: 1

Related Questions