我想解析JSON字符串,以便将其保存为二维数组,如下所示。只有我带来的解决方案包括3个或4个foreach循环,我不认为这很好。
{
"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 :(得分:2)
$json = json_decode($jsonString, true);
$result = [];
array_walk_recursive($json, function ($value, $key) use (&$result) {
$result[$key][] = $value;
});