简体   繁体   English

解析PHP中的json键值(多维数组)

[英]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. 到目前为止,我认为我'尝试'了解所有建议的方法来解析POST响应中的一个或多个键的值。 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). 我的目标是检索“id”和“campaign_id”键的值(或任何其他键值)。 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!! 谢谢@EatPienutButter帮助我!

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']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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