jmanfffreak Posted August 23, 2010 Share Posted August 23, 2010 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 https://forums.phpfreaks.com/topic/211505-using-an-array-to-call-another-array/ Share on other sites More sharing options...
AbraCadaver Posted August 23, 2010 Share Posted August 23, 2010 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 https://forums.phpfreaks.com/topic/211505-using-an-array-to-call-another-array/#findComment-1102742 Share on other sites More sharing options...
jmanfffreak Posted August 23, 2010 Author Share Posted August 23, 2010 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 https://forums.phpfreaks.com/topic/211505-using-an-array-to-call-another-array/#findComment-1102750 Share on other sites More sharing options...
AbraCadaver Posted August 23, 2010 Share Posted August 23, 2010 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 https://forums.phpfreaks.com/topic/211505-using-an-array-to-call-another-array/#findComment-1102757 Share on other sites More sharing options...
jmanfffreak Posted August 23, 2010 Author Share Posted August 23, 2010 Aha, I know what was wrong. I was doing it right the entire time, but I was including a page within a page, and was doing it wrong. Link to comment https://forums.phpfreaks.com/topic/211505-using-an-array-to-call-another-array/#findComment-1102832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.