我有下面的JSON结构,我作为$ _POST请求进入我的PHP脚本。广告系列名称和组名称的密钥是动态生成的,因此我需要提取这些密钥,以便我可以将每个广告系列的组名称和广告文案分组到单独的数组中。
我在这里看到的几个帖子非常相似,但到目前为止这些解决方案都没有奏效。我觉得我过于复杂了。
{
"First Campaign Name": {
"First Group Name": {
"textads": [
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
],
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
]
]
},
"Second Group Name": {
"textads": [
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
],
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
]
]
}
},
"Second Campaign Name": {
"First Group Name": {
"textads": [
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
],
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
]
]
},
"Second Group Name": {
"textads": [
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
],
[
"{Headline text}",
"First line text",
"Second line text",
"Display URL"
]
]
}
}
}
我尝试了几个foreach和嵌套的foreach循环,试图在没有运气的情况下访问键和值。我最近的尝试是:
foreach($jsonString as $campaign) {
foreach($campaign as $k => $v){
$postData .= "Key: " . $k . "\r\n";
$postData .= "Val: " . $v . "\r\n";
}
}
其中$ postData已定义,然后保存到文件中以进行输出。 我还尝试了以下方法:
foreach ($_POST as $key => $value) {
foreach( $value as $item){
foreach( $item as $k => $v){
$postData .= "Key: " . $k . "\r\n";
$postData .= "Val: " . $v . "\r\n";
}
}
}
答案 0 :(得分:2)
在将它与foreach一起使用之前,您需要将JSON转换为php数组。
$ phparray = json_decode($ jsonString);