0

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";
        }
    }
}
Mousey
  • 1,855
  • 19
  • 34
Jonathan
  • 323
  • 2
  • 11
  • that's about what you'd need. loops nested 3 deep. how is this not working? – Marc B Aug 12 '15 at 19:22
  • Maybe it's just me, but I'm having kind of a hard time understanding what the structure you're going for would be. Could you include an example of the output you are trying to get? – Don't Panic Aug 12 '15 at 19:22
  • So it turns out the issue wasn't my logic after all but rather the actual JSON that the script was receiving which I can't post here because it contains sensitive information. The JSON data that is being passed contains single quotes that aren't escaped properly. Thanks for everyone's help! – Jonathan Aug 13 '15 at 17:44
  • To clarify my previous comment above, the JSON data being received through $_POST contains apostrophe's which appear to be breaking up the string. – Jonathan Aug 13 '15 at 17:54

1 Answers1

2

You will need to convert the JSON to a php array before you can use it with a foreach.

$phparray = json_decode ($jsonString);

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

Jason K
  • 1,406
  • 1
  • 12
  • 15
  • `json_decode ($jsonString);` will return an object. You will need to add an additional flag to get an assoc array: `json_decode ($jsonString, true)` – HPierce Aug 12 '15 at 20:25