简体   繁体   中英

Parsing json key value in PHP (multidimensional array)

By now I think I 'tried' just about all of the suggested ways to parse the value of one or more keys from a POST response. I've been able to boil it down a little but I'd super appreciate any help someone would be willing to give. My goal is to retrieve the value of the "id" and "campaign_id" keys (or any other key value for that matter). Needless to say I'm at a beginners level ˆ_ˆ

here we go..

$contents = ($this->response);
$enc = ($contents);

results into:

Array ( [0] => [ { "url": "http://www.aguabenito.com", "name": "Bikinis - New arrivals", "prefix": "AGUA", "notes": "", "updated_at": "2017-01-14 16:26:35", "created_at": "2017-01-14 16:26:35", "id": 4609 }, [], [ { "id": 3531, "url_code": "R0uvzO", "alias": null, "campaign_id": 4609, "paidchannel_id": 104, "deleted_at": null, "created_at": "2017-01-14 16:26:35", "updated_at": "2017-01-14 16:26:35" } ] ] ) 

and then..

for ($i = 0; $i < count($enc); ++$i) {
    print $enc[$i];
}

results into:

[
    {
        "url": "http://www.aguabenito.com",
        "name": "Bikinis - New arrivals",
        "prefix": "AGUA",
        "notes": "",
        "updated_at": "2017-01-14 16:26:35",
        "created_at": "2017-01-14 16:26:35",
        "id": 4609
    },
    [],
    [
        {
            "id": 3531,
            "url_code": "R0uvzO",
            "alias": null,
            "campaign_id": 4609,
            "paidchannel_id": 104,
            "deleted_at": null,
            "created_at": "2017-01-14 16:26:35",
            "updated_at": "2017-01-14 16:26:35"
        }
    ]

I'm afraid to say that I'm just going around in circles from here onwards. When I try to get any of the values I keep getting Illegal string offset or Undefined index errors. Really hoping to learn what I'm doing wrong and how I should go about retrieving the value of one or more of these keys.

Hoping to achieve something in the lines of:

$campaign_id = '4609';
$first_urlcode = 'R0uvzO';
$first_urlcode_id = '3531';
$second_urlcode = 'abc123';
$second_urlcode_id = '1234';

Thanks @EatPienutButter for helping me out!!

This got me walking straight again )))))

$enc = json_decode($contents[0], true);

$campaignid = ($enc[0]['id']);
$first_urlcode = ($enc[2][0]['url_code']);
$first_urlcode_id = ($enc[2][0]['id']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM