1

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';
5
  • 1
    Why not start with json_decode()? Commented Jan 14, 2017 at 17:13
  • @EatPeanutButter, $enc = json_decode($contents); give the error json_decode() expects parameter 1 to be string, array given Commented Jan 14, 2017 at 17:25
  • json_decode($contents[0]) Commented Jan 14, 2017 at 17:30
  • thanks @EatPeanutButter but then fails because Object of class stdClass could not be converted to string Commented Jan 14, 2017 at 17:38
  • Ah, the true parameter helped though $enc = json_decode($contents[0], true); Commented Jan 14, 2017 at 17:43

1 Answer 1

0

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']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.