2016-11-14 234 views
1

我想解析JSON字符串,以便我可以將它保存到如下所示的二維數組中。我唯一的解決方案是由3或4個foreach循環組成,我不認爲這很好。解析json字符串到數組

{ 
    "root": { 
    "row": [ 
     { 
     "Status": "Enabled", 
     "Keyword": "Toaletna voda", 
     "Campaign": "Lešenari", 
     "Adgroup": "Lešenaris", 
     "BidStrategyType": "InheritFromParent", 
     "Bid": "0.05", 
     "Matchtype": "Broad", 
     "Clicks": "0", 
     "Impr.": "0", 
     "Conv.": "0" 
     }, 
     { 
     "Status": "Enabled", 
     "Keyword": "lyžička", 
     "Campaign": "Lešenari", 
     "Adgroup": "Lešenaris", 
     "BidStrategyType": "InheritFromParent", 
     "Bid": "0.05", 
     "Matchtype": "Broad", 
     "Clicks": "0", 
     "Impr.": "0", 
     "Conv.": "0" 
     }, 
     { 
     "Status": "Search total", 
     "Keyword": "-", 
     "Campaign": "-", 
     "Adgroup": "-", 
     "BidStrategyType": "-", 
     "Bid": "-", 
     "Matchtype": "-", 
     "Clicks": "0", 
     "Impr.": "0", 
     "Conv.": "0" 
     }, 
     { 
     "Status": "Content total", 
     "Keyword": "-", 
     "Campaign": "-", 
     "Adgroup": "-", 
     "BidStrategyType": "-", 
     "Bid": "-", 
     "Matchtype": "-", 
     "Clicks": "0", 
     "Impr.": "0", 
     "Conv.": "0" 
     }, 
     { 
     "Status": "Deleted items total", 
     "Keyword": "-", 
     "Campaign": "-", 
     "Adgroup": "-", 
     "BidStrategyType": "-", 
     "Bid": "-", 
     "Matchtype": "-", 
     "Clicks": "0", 
     "Impr.": "0", 
     "Conv.": "0" 
     }, 
     { 
     "Status": "Overall total", 
     "Keyword": "-", 
     "Campaign": "-", 
     "Adgroup": "-", 
     "BidStrategyType": "-", 
     "Bid": "-", 
     "Matchtype": "-", 
     "Clicks": "0", 
     "Impr.": "0", 
     "Conv.": "0" 
     } 
    ] 
    } 
} 

它應該返回像這樣

Keyword=>Toaletna voda 
Keyword=>lyžička 
Campaign=>Lešenari 
Campaign=>Lešenari 
Adgroup=>Lešenaris 
Adgroup=>Lešenaris 
Bid=>0.05 
Bid=>0.05 
Clicks=>0 
Clicks=>0 
Impr.=>0 
Impr.=>0 
Conv.=>0 
Conv.=>0 

應該做同樣的事情,因爲這功能,它需要名稱的數組,發現被追加到它在XML文件中的值

public function LoadXmlReport($adSystemColumns = array()) 
    { 
     require "config.php"; 

     $xmlfile = simplexml_load_file(dirname(__FILE__) . "/xmlfile.xml"); 
     foreach ($xmlfile as $key => $value) 
      foreach ($value as $columnName => $item) { 
       if ($item == "-") { 
        break; 
       } elseif (array_search($columnName, $bing) !== FALSE) 
        $this->report[$columnName][] = $item; 
      } 

     foreach ($this->report as $key => $value) 
      foreach ($value as $index => $item) 
       echo $key."=>".$item."<br/>"; 
    } 

那些值

$bing = array(
"Adgroup", 
"Campaign", 
"Keyword", 
"Clicks", 
"Impr.", 
"Conv.", 
"Bid", 
"Adgroup" 
); 
+0

http://php.net/manual/en/function.json-decode.php? –

+0

好的,這個問題可能是不正確的。我使用json_decode,但後來我得到了需要3或4個嵌套foreach循環的東西,我需要幫助。 – rtom

+0

您的期望輸出仍不清楚。它是多個相同的鍵或數組對的字典? –

回答

2
$json = json_decode($jsonString, true); 
$result = []; 
array_walk_recursive($json, function ($value, $key) use (&$result) { 
    $result[$key][] = $value; 
}); 
+0

謝謝,這看起來像一個有趣的功能,我們會看到如果我可以帶一些工作代碼。 – rtom