2013-04-14 94 views
0

這裏是字符串..腓複雜爆炸

$string = "foo1 : bar1, foo2: bar2, foo3: bar3"; 

使用,定界符

$exploded = (",", $string); 

現在$exploded數組包含爆炸:

foo1 : bar1
foo2 : bar2
foo3 : bar3

現在我需要把foo1array['key']bar1array['value']

如何實現這一目標?

+0

這看起來像你正在解析_sort-of_ json - 是嗎? – AD7six

+0

JSON有點複雜。會有'bar'值的引號。 –

+0

@ AD7six nop從數據庫中刪除 –

回答

3

你需要創建另一個循環都要經過"foo:bar"字符串數組和爆炸他們:

$exploded = explode(",", $input); 
$output = array();  //Array to put the results in 
foreach($exploded as $item) { //Go through "fooX : barX" pairs 
    $item = explode(" : ", $item); //create ["fooX", "barX"] 
    $output[$item[0]] = $item[1]; //$output["fooX"] = "barX"; 
} 
print_R($output); 

注意,如果同一個鍵多次出現在輸入字符串 - 他們相互之間會覆蓋其他並且只有最後一個值將存在於結果中。

+0

嗯..好..謝謝 –