I have the JSON structure below that I'm pulling into my PHP script as a $_POST request. The keys for the Campaign Names and Group Names are dynamically generated so I need to extract those keys so I can then group each campaign with it's group name and ad copy into separate arrays.
I've seen several posts here that are very similar but none of those solutions have worked so far. I feel I'm over-complicating this.
{
"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"
]
]
}
}
}
I've tried several foreach and nested foreach loops in trying to access the keys and values with no luck. My most recent attempts are:
foreach($jsonString as $campaign) {
foreach($campaign as $k => $v){
$postData .= "Key: " . $k . "\r\n";
$postData .= "Val: " . $v . "\r\n";
}
}
Where $postData is already defined and then saved to a file for output. I also attempted the following:
foreach ($_POST as $key => $value) {
foreach( $value as $item){
foreach( $item as $k => $v){
$postData .= "Key: " . $k . "\r\n";
$postData .= "Val: " . $v . "\r\n";
}
}
}