PHP: Array path

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function getValue($sPath$aArray)
{
    foreach(explode('/'$sPath) as $sKey)
    $aArray=&$aArray[$sKey];
    return $aArray;
}
$aConfiguration = array();
$aConfiguration['server']['host'] = "localhorst";
$aConfiguration['server']['user'] = "admin";
$aConfiguration['server']['password'] = "supersicher";
$aConfiguration['directories']['home']['user1'] = array('Files''Docs''Pictures');
$aConfiguration['directories']['home']['user2'] = array('Backup''Code''Projects');
$aConfiguration['directories']['home']['user3'] = array('Music''Games''Pictures');
$aPaths = array('directories/home/user1',
                'directories/home/user2',
                'directories/home/user3',
                'server/host',
                'server/user',
                'server/password'
);
 
foreach($aPaths as $sPath)
{
    print_r(getValue($sPath$aConfiguration));
    echo "<br/>";
}
/*
    Ausgabe:
    Array ( [0] => Files [1] => Docs [2] => Pictures )
    Array ( [0] => Backup [1] => Code [2] => Projects )
    Array ( [0] => Music [1] => Games [2] => Pictures )
    localhorst
    admin
    supersicher
 */
X

Url: http://sklueh.de/2013/08/php-array-path/

Language: PHP | User: sklueh | Created: Sep 24, 2013