Accessing value of an array, using an array as the indices
Posted: Tue Nov 22, 2011 8:06 am
Hey there,
It's been a while since I posted here. Few years probably. I'm working on a project, and it would be really helpful if I could figure out a simple way of doing what I am about to describe.
What I am trying to do, is use array values as the access keys to another array, it will be easier for me to show you.Basically, I will have a large array that holds all of my config values, and because of how I have set my project up, I will get a lot of results like $keys in the above. What I would like to be able to do is figure out how to use $keys to map me to the result "messages_table" without using any sort of loop. So basically, I want $keys to become the ['db']['tables']['messages'] that I would throw onto the end of $config;
Temporarily, I've used this:
Have I explained that adequately? It's a little difficult for me to explain, and it's been a long night for me so I might not be thinking sharply, but I am hoping someone here is maybe able to open a few windows for me and shine some light on an easy solution.
Thanks for any help,
Jeff
It's been a while since I posted here. Few years probably. I'm working on a project, and it would be really helpful if I could figure out a simple way of doing what I am about to describe.
What I am trying to do, is use array values as the access keys to another array, it will be easier for me to show you.
Code: Select all
$keys = array("db", "tables", "messagse");
$config = array(
"db" => array(
"tables" => array(
"messages" => "messages_table",
"users" => "users_table",
),
"settings" => array(
"username" => "db username",
"password" => "db password",
),
),
"lang" => array(
"etc......"
)
);
Temporarily, I've used this:
Code: Select all
$item = $config;
foreach($keys as $k)
{
$item = $item[$k];
}
// $item = "messages_table" (expected result)
Thanks for any help,
Jeff