使用PHP访问多维JSON数组中的值

时间:2021-01-05 21:29:34

I read several posts on here and have been unable to find success in resolving the problem I am having.

我在这里阅读了几篇帖子,并且无法在解决我遇到的问题方面找到成功。

I am trying to figure out how to get the NAME field from the UploadImage array.

我试图弄清楚如何从UploadImage数组中获取NAME字段。

I have the following JSON being passed to me from a Webhook.

我有一个从Webhook传递给我的以下JSON。

{
"$version":5,
"Entry":{
    "Number":"11",
    "Order":null,
    "Origin":
        {
        "City":"Portland",
        "CountryCode":"US",
        }
        ,
"Message":"the message",
"UploadImage":[
    {
        "ContentType":"image/png",
        "Id":"F-lMbiCYdwiYS8ppkQS4gsyE",
        "Name":"Screen.png",
        "Size":55907
    }
            ],
"Subject":"here is the subject" 
}

I have no problem grabbing the value of Subject or Message, but I cannot figure out how to grab the NAME within UploadImage.

我没有问题抓住主题或消息的价值,但我无法弄清楚如何在UploadImage中获取NAME。

  $contact = json_decode($json);  
  $subject=$contact->{'Subject'};

When I do

当我做

    $uploadimage=$contact->{'UploadImage'};  

it just writes out ARRAY.

它只是写出了ARRAY。

I can do

我可以

 echo  $contact->{'Entry'}->{'Number'}; 

and it works, so it has to be something with the bracket being there before the curly bracket. I know this has to be something simple that I am missing. Any help is GREATLY appreciated.

并且它起作用,所以它必须是括号在大括号之前存在的东西。我知道这必须是我想念的简单事物。任何帮助是极大的赞赏。

6 个解决方案

#1


1  

$uploadimage=$contact->{'UploadImage'}[0]->{'Name'};

#2


1  

Firstly try

$contact = json_decode($json, true);

Adding the second argument returns an array instead of an object which will make things easier. Objects with numerical keys are troublesome... Now you can,

添加第二个参数将返回一个数组而不是一个对象,这将使事情变得更容易。带数字键的对象很麻烦......现在你可以,

print_r($contact);

to see exactly what you've got. I imagine that

准确地看到你所拥有的。我想象

echo $contact['UploadImage'][0]['Name'];

Will get you what you're looking for.

会得到你想要的东西。

Notice that UploadImage contains an array of objects (or an array of arrays after conversion).

请注意,UploadImage包含一个对象数组(或转换后的数组数组)。

#3


1  

another solution is:

另一个解决方案是

$contact = json_decode($text);
$name = '';
foreach($contact->UploadImage as $k=>$v){
    foreach($v as $k2=>$v2){
        echo $k2.' - '.$v2.'<br />';
        if($k2=='Name'){ $name = $v2;}
    }

};
var_dump($name);

//response

ContentType - image/png
Id - F-lMbiCYdwiYS8ppkQS4gsyE
Name - Screen.png
Size - 55907
//name
string 'Screen.png' (length=10)

#4


0  

Have you try to use like below:

您是否尝试使用如下所示:

$uploadimage=$contact->UploadImage[0]->Name;  

#5


0  

In JSON the square braces [] mark an array section and the curly braces {} mark an object. In your JSON string UploadImage is an array, which contains a single object.

在JSON中,方括号[]标记数组部分,花括号{}标记对象。在您的JSON字符串中,UploadImage是一个包含单个对象的数组。

#6


-2  

Try changing your JSON as follows:

尝试更改您的JSON,如下所示:

{
    "$version": 5,
    "Entry": {
        "Number": "11",
        "Order": null,
        "Origin": {
            "City": "Portland",
            "CountryCode": "US"
        },
        "Message": "the message",
        "UploadImage": {
            "ContentType": "image/png",
            "Id": "F-lMbiCYdwiYS8ppkQS4gsyE",
            "Name": "Screen.png",
            "Size": 55907
        },
        "Subject": "here is the subject"
    }
}

#1


1  

$uploadimage=$contact->{'UploadImage'}[0]->{'Name'};

#2


1  

Firstly try

$contact = json_decode($json, true);

Adding the second argument returns an array instead of an object which will make things easier. Objects with numerical keys are troublesome... Now you can,

添加第二个参数将返回一个数组而不是一个对象,这将使事情变得更容易。带数字键的对象很麻烦......现在你可以,

print_r($contact);

to see exactly what you've got. I imagine that

准确地看到你所拥有的。我想象

echo $contact['UploadImage'][0]['Name'];

Will get you what you're looking for.

会得到你想要的东西。

Notice that UploadImage contains an array of objects (or an array of arrays after conversion).

请注意,UploadImage包含一个对象数组(或转换后的数组数组)。

#3


1  

another solution is:

另一个解决方案是

$contact = json_decode($text);
$name = '';
foreach($contact->UploadImage as $k=>$v){
    foreach($v as $k2=>$v2){
        echo $k2.' - '.$v2.'<br />';
        if($k2=='Name'){ $name = $v2;}
    }

};
var_dump($name);

//response

ContentType - image/png
Id - F-lMbiCYdwiYS8ppkQS4gsyE
Name - Screen.png
Size - 55907
//name
string 'Screen.png' (length=10)

#4


0  

Have you try to use like below:

您是否尝试使用如下所示:

$uploadimage=$contact->UploadImage[0]->Name;  

#5


0  

In JSON the square braces [] mark an array section and the curly braces {} mark an object. In your JSON string UploadImage is an array, which contains a single object.

在JSON中,方括号[]标记数组部分,花括号{}标记对象。在您的JSON字符串中,UploadImage是一个包含单个对象的数组。

#6


-2  

Try changing your JSON as follows:

尝试更改您的JSON,如下所示:

{
    "$version": 5,
    "Entry": {
        "Number": "11",
        "Order": null,
        "Origin": {
            "City": "Portland",
            "CountryCode": "US"
        },
        "Message": "the message",
        "UploadImage": {
            "ContentType": "image/png",
            "Id": "F-lMbiCYdwiYS8ppkQS4gsyE",
            "Name": "Screen.png",
            "Size": 55907
        },
        "Subject": "here is the subject"
    }
}