lusketeer
lusketeer

Reputation: 1930

Retrieve Value from PHP Array

I have an array to store the configuration like this

$config = array (
            'db' => array(
                'db1' => array(
                    'dbname' => 'mydatabase', 
                    'user' => 'myusername', 
                    'pass' => 'mypassword', 
                    'host' => 'myhost'
                )
            ),
            'url' => array(
                'homeUrl' => 'http://www.example.com'
            )
         )

And I'm writing a function to retrieve the data from the array by passing in a string like db.db1.dbname and it supposes to give me 'mydatabase'

I tried to explode the string into an array in order to get the keys, db, db1, and dbname. but after that, I got kinda stuck on how exactly I'm supposed to use them like $config -> db -> db1 -> dbname or $config['db']['db1']['dbname'] in order to get 'mydatabase'.

Ideally, say I have the function named read($arg, $array), and I would like to retrieve results like this

read('db.db1.dbname', $config), returns 'mydatabase'
read('url.homeUrl', $config), returns 'http://www.example.com'

Since I don't know how many keys are contained in the string, I need this to be more dynamic. Thanks in advance

Upvotes: 0

Views: 211

Answers (5)

FrankieTheKneeMan
FrankieTheKneeMan

Reputation: 6800

function read($layers, $arr){
    $toReturn = $arr;
    foreach(split('.', $layers) as $layer)
        $toReturn = $toReturn[$layer];
    return $toReturn;
}

Why would you want to do this, I haven't the foggiest. But there you go.

Upvotes: 0

PeeHaa
PeeHaa

Reputation: 72672

I'm really wondering why you would want to do this, but here it goes:

function read($item, $config)
{
    $selectors = explode('.', $item);

    $configItem = $config;
    foreach($selectors as $selector) {
        $configItem = $configItem[$selector];
    }

    return $configItem;
}

$config = array (
    'db' => array(
        'db1' => array(
            'dbname' => 'mydatabase', 
            'user' => 'myusername', 
            'pass' => 'mypassword', 
            'host' => 'myhost',
        ),
    ),
    'url' => array(
        'homeUrl' => 'http://www.example.com',
    ),
);

read('db.db1.dbname', $config); // will return mydatabase

Note that you would have to check whether the keys exists and throw an error or exception if that's not the case.

Instead of using the function is there a reason why you cannot do:

function functionThatNeedsDatabaseInfo($databaseInfo)
{
    // do database stuff
}

functionThatNeedsDatabaseInfo($config['db']['db1']);

Upvotes: 4

stan
stan

Reputation: 4995

They are just plain nested arrays.

$config['db'] will give you the first inner array

$config['db']['db1'] will give you the array with all the database configurations you're looking for.

$config['db']['db1']['dbname']: 'dbname' is the index in the 'db1' array that will give you the value you want.

Upvotes: 1

FrankieTheKneeMan
FrankieTheKneeMan

Reputation: 6800

Cast that thing to an Object! Or rather, cast every array as an object, then you can use it like one.

$config = (object) array (
        (object) 'db' => array(
            (object) 'db1' => array(
                'dbname' => 'mydatabase', 
                'user' => 'myusername', 
                'pass' => 'mypassword', 
                'host' => 'myhost'
            )
        ),
        (object) 'url' => array(
            'homeUrl' => 'http://www.example.com'
        )
     )

Upvotes: -1

romo
romo

Reputation: 1990

I think you're thinking about JSON

try $config['db']['db1']['dbname'];

Upvotes: 5

Related Questions