Jump to content

Using an array to call another array.


jmanfffreak

Recommended Posts

Hi,

 

I have the following code in one of my configuration files of the application I'm writing:

 

// config.php

$config = array(
'IndexPage' => "$config['MenuItems']['Main Menu']['Home']";   // What page should the application default to if no action is specified?
'MenuItems' => array(
     'Main Menu' => array(
          'Home' => array('module' => 'main', 'action' => 'index');

 

Essentially, $config['IndexPage'] can change if a user wants a different page when users access the system. I need to basically call the $config['IndexPage'] array as if it were an actual array, and stick it straight into my page. Is there anyway this can be done?

Link to comment
Share on other sites

Since $config hasn't been completely defined when you try and use it inside of itself, you'll need something like this:

 

$config = array(
'MenuItems' => array(
     'Main Menu' => array(
          'Home' => array('module' => 'main', 'action' => 'index'))));

$config['IndexPage'] = $config['MenuItems']['Main Menu']['Home'];

print_r($config['IndexPage']);

 

For legibility and ease of use, I tend to define the arrays like this:

 

$config['MenuItems']['Main Menu']['Home'] = array('module' => 'main', 'action' => 'index');
$config['IndexPage'] = $config['MenuItems']['Main Menu']['Home'];

Link to comment
Share on other sites

Well, for some odd reason, that doesnt show much. I have that array above in my config file...it was shortened, I apologize for not closing everything properly.

 

Basically, I have a function that includes /modules/main/index (or is supposed to) in the event that someone just goes to the root directory and/or someone tries to access a page directly. The user can set the system up so that instead of the default page being in /modules/main/index, it can be /modules/main/about or something similar, and this is all controlled by arrays. Is there an easy way of doing this? I have the following setup:

 

$config['IndexPage'] = $config['MenuItems']['Main Menu']['Home'], // Default page that user will be directed to.
$config['MenuItems']['Main Menu']['Home'] = array('module' => 'main', 'action' => 'index');

 

In a totally seperate file, I need to call the variable $config['IndexPage'] as it was just bringing the text straight from the config file, and then stick the last part of the final array on, which would be ['module'] and ['action'] and process it like it was an actual array.

 

Right now, I have the following code calling the array into the code:

$home = print_r($myst['IndexPage']);
$home = "modules/{$home['module']}/{$home['action']}";
include("{$home}.php");

 

This, however, returns the following :

 

Warning: include(/modules//.php) failed to open, no files or directory, etc etc.

 

Is there an easier way to do this, or what am I missing here? Thanks for any help. :)

 

Link to comment
Share on other sites

The print_r() was just there to show you that the array would be printed out succesfully, it shouldn't be part of your code.  First you have the config definitions backwards.  You have to assign something to $config['MenuItems']['Main Menu']['Home'] before you try and assign that to $config['IndexPage'].  If I understand correctly, your files should look like this:

 

//config.php
//this needs to be first because you use it in the next line:
$config['MenuItems']['Main Menu']['Home'] = array('module' => 'main', 'action' => 'index');
$config['IndexPage'] = $config['MenuItems']['Main Menu']['Home'];

 

//other_file.php
include('config.php');
$home = "modules/{$config['IndexPage']['module']}/{$config['IndexPage']['action']}";
include("{$home}.php");

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.