2016-06-06 5 views

Antwort

5
$str = "eat.fruit.apple.good"; 
// Set pointer at top of the array 
$arr = array(); 
$path = &$arr; 
// Path is joined by points 
foreach (explode('.', $str) as $p) 
    // Make a next step 
    $path = &$path[$p]; 
$path = true; 
print_r($arr); // $arr["eat"]["fruit"]["apple"]["good"] = true; 

UPDATE Variante ohne Zeiger:

$str = "eat.fruit.apple.good"; 

$res = 'true'; 
foreach(array_reverse(explode('.', $str)) as $i) 
    $res = '{"' . $i . '":' . $res . '}'; 
$arr = json_decode($res, true); 
+0

Sehr saubere Lösung! –

+0

Nicht schlecht ... – DonCallisto

+0

Was für ein Genie. Danke, Mann. Wie kann ich das machen, wenn ich keine Zeiger benutzen will? – Davide

Verwandte Themen