2015-09-10 32 views
0

目標: 循環訪問字符串並將自定義對象存儲在數組中,以保留嵌套特性。PHP嘗試從字符串中捕獲數組中的嵌套標記

字符串和數組結構的示例如下。

我已經嘗試過遞歸正則表達式,但找不到解決方案,因爲它不是真正爲此做的(從我研究過的)。

這與html/xml解析器類似,但用於自定義標籤。

//Array structure 
$object["number"]; 
$object["children"]; 

    for($i = 0; $<$stringTotal; $i++ { 
    $char = $string[$i]; 

    if ($char == "[") { 
    //start new object 
    } 

    if ($char == "]") { 
    //finish current object 
    } 
    } 

Example: 
String = "[1] 
[2 
    [2.a] 
    [2.b 
    [2.c] 
    ] 
]" 
+0

我覺得'爆炸()'會有所幫助。你可以添加更多的細節,你想要完成什麼? – mattslone

+0

我更新了問題,希望它更容易理解。 –

回答

0

它看起來像你需要一些遞歸...

foreach($something as $key => $value) { 
    $something = do_with_something($value); 
} 

function do_with_something($recvalue) 
{ 
    if (is_array($recvalue)) 
    { 
     foreach($recvalue as $key => $value) 
     { 
      $recvalue[$key] = do_with_something($value); 
     } 
    } 
    else 
    { 
     /* do whatever you want to do and pass it back */ 
     $recvalue = $whater_you_want_to_pass_back"; 
    } 
    return $recvalue; 
}