我正在尝试在解码数组上运行foreach,如下所示:
阵列:
[
{
"addresses": [
{
"city": "Hod Hash",
"country": "Israel",
"countryCode": "",
"localizedLabel": "work",
"originalLabel": "_$!<Work>!$_",
"state": "",
"street": "Shahaf6\nSec",
"zip": 41343
}
],
"birthday": "2006-12-2712: 00: 00+0000",
"creationDate": "2016-12-2711: 30: 00+0000",
"emails": [
{
"address": "nir@kfs.fin",
"localizedLabel": "Lab",
"originalLabel": "Lab"
},
{
"address": "furt@hdf.cin",
"localizedLabel": "iCloud",
"originalLabel": "iCloud"
}
],
"job": {
"company": "Layer",
"jobTitle": ""
},
"modificationDate": "2016-12-2711: 30: 00+0000",
"name": {
"compositeName": "Test Contant",
"firstName": "Test",
"lastName": "Contant",
"middleName": ""
},
"notes": "Guy yd he hcgd g\n",
"phones": [
{
"localizedLabel": "home",
"number": "054-8862488",
"originalLabel": "_$!<Home>!$_"
},
{
"localizedLabel": "iPhone",
"number": "054-7532635",
"originalLabel": "iPhone"
},
{
"localizedLabel": "work",
"number": "03-4214532",
"originalLabel": "_$!<Work>!$_"
}
],
"recordId": "908",
"source": {
"sourceID": 0,
"sourceType": ""
},
"websites": [
"www.layer.com",
"google.com"
]
},
{
"addresses": [
{
"city": "Hod Hash",
"country": "Israel",
"countryCode": "",
"localizedLabel": "work",
"originalLabel": "_$!<Work>!$_",
"state": "",
"street": "Shahaf6\nSec",
"zip": 41343
}
],
"birthday": "2006-12-2712: 00: 00+0000",
"creationDate": "2016-12-2711: 30: 00+0000",
"emails": [
{
"address": "nir@kfs.fin",
"localizedLabel": "Lab",
"originalLabel": "Lab"
},
{
"address": "furt@hdf.cin",
"localizedLabel": "iCloud",
"originalLabel": "iCloud"
}
],
"job": {
"company": "Layer",
"jobTitle": ""
},
"modificationDate": "2016-12-2711: 30: 00+0000",
"name": {
"compositeName": "Test Contant",
"firstName": "Test",
"lastName": "Contant",
"middleName": ""
},
"notes": "Guy yd he hcgd g\n",
"phones": [
{
"localizedLabel": "home",
"number": "054-8862488",
"originalLabel": "_$!<Home>!$_"
},
{
"localizedLabel": "iPhone",
"number": "054-7532635",
"originalLabel": "iPhone"
},
{
"localizedLabel": "work",
"number": "03-4214532",
"originalLabel": "_$!<Work>!$_"
}
],
"recordId": "908",
"source": {
"sourceID": 0,
"sourceType": ""
},
"websites": [
"www.layer.com",
"google.com"
]
},
{
"addresses": [
{
"city": "Hod Hash",
"country": "Israel",
"countryCode": "",
"localizedLabel": "work",
"originalLabel": "_$!<Work>!$_",
"state": "",
"street": "Shahaf6\nSec",
"zip": 41343
}
],
"birthday": "2006-12-2712: 00: 00+0000",
"creationDate": "2016-12-2711: 30: 00+0000",
"emails": [
{
"address": "nir@kfs.fin",
"localizedLabel": "Lab",
"originalLabel": "Lab"
},
{
"address": "furt@hdf.cin",
"localizedLabel": "iCloud",
"originalLabel": "iCloud"
}
],
"job": {
"company": "Layer",
"jobTitle": ""
},
"modificationDate": "2016-12-2711: 30: 00+0000",
"name": {
"compositeName": "Test Contant",
"firstName": "Test",
"lastName": "Contant",
"middleName": ""
},
"notes": "Guy yd he hcgd g\n",
"phones": [
{
"localizedLabel": "home",
"number": "054-8862488",
"originalLabel": "_$!<Home>!$_"
},
{
"localizedLabel": "iPhone",
"number": "054-7532635",
"originalLabel": "iPhone"
},
{
"localizedLabel": "work",
"number": "03-4214532",
"originalLabel": "_$!<Work>!$_"
}
],
"recordId": "908",
"source": {
"sourceID": 0,
"sourceType": ""
},
"websites": [
"www.layer.com",
"google.com"
]
},
{
"addresses": [
{
"city": "Hod Hash",
"country": "Israel",
"countryCode": "",
"localizedLabel": "work",
"originalLabel": "_$!<Work>!$_",
"state": "",
"street": "Shahaf6\nSec",
"zip": 41343
}
],
"birthday": "2006-12-2712: 00: 00+0000",
"creationDate": "2016-12-2711: 30: 00+0000",
"emails": [
{
"address": "nir@kfs.fin",
"localizedLabel": "Lab",
"originalLabel": "Lab"
},
{
"address": "furt@hdf.cin",
"localizedLabel": "iCloud",
"originalLabel": "iCloud"
}
],
"job": {
"company": "Layer",
"jobTitle": ""
},
"modificationDate": "2016-12-2711: 30: 00+0000",
"name": {
"compositeName": "Test Contant",
"firstName": "Test",
"lastName": "Contant",
"middleName": ""
},
"notes": "Guy yd he hcgd g\n",
"phones": [
{
"localizedLabel": "home",
"number": "054-8862488",
"originalLabel": "_$!<Home>!$_"
},
{
"localizedLabel": "iPhone",
"number": "054-7532635",
"originalLabel": "iPhone"
},
{
"localizedLabel": "work",
"number": "03-4214532",
"originalLabel": "_$!<Work>!$_"
}
],
"recordId": "908",
"source": {
"sourceID": 0,
"sourceType": ""
},
"websites": [
"www.layer.com",
"google.com"
]
},
{
"addresses": [
{
"city": "Hod Hash",
"country": "Israel",
"countryCode": "",
"localizedLabel": "work",
"originalLabel": "_$!<Work>!$_",
"state": "",
"street": "Shahaf6\nSec",
"zip": 41343
}
],
"birthday": "2006-12-2712: 00: 00+0000",
"creationDate": "2016-12-2711: 30: 00+0000",
"emails": [
{
"address": "nir@kfs.fin",
"localizedLabel": "Lab",
"originalLabel": "Lab"
},
{
"address": "furt@hdf.cin",
"localizedLabel": "iCloud",
"originalLabel": "iCloud"
}
],
"job": {
"company": "Layer",
"jobTitle": ""
},
"modificationDate": "2016-12-2711: 30: 00+0000",
"name": {
"compositeName": "Test Contant",
"firstName": "Test",
"lastName": "Contant",
"middleName": ""
},
"notes": "Guy yd he hcgd g\n",
"phones": [
{
"localizedLabel": "home",
"number": "054-8862488",
"originalLabel": "_$!<Home>!$_"
},
{
"localizedLabel": "iPhone",
"number": "054-7532635",
"originalLabel": "iPhone"
},
{
"localizedLabel": "work",
"number": "03-4214532",
"originalLabel": "_$!<Work>!$_"
}
],
"recordId": "908",
"source": {
"sourceID": 0,
"sourceType": ""
},
"websites": [
"www.layer.com",
"google.com"
]
},
{
"addresses": [
{
"city": "Hod Hash",
"country": "Israel",
"countryCode": "",
"localizedLabel": "work",
"originalLabel": "_$!<Work>!$_",
"state": "",
"street": "Shahaf6\nSec",
"zip": 41343
}
],
"birthday": "2006-12-2712: 00: 00+0000",
"creationDate": "2016-12-2711: 30: 00+0000",
"emails": [
{
"address": "nir@kfs.fin",
"localizedLabel": "Lab",
"originalLabel": "Lab"
},
{
"address": "furt@hdf.cin",
"localizedLabel": "iCloud",
"originalLabel": "iCloud"
}
],
"job": {
"company": "Layer",
"jobTitle": ""
},
"modificationDate": "2016-12-2711: 30: 00+0000",
"name": {
"compositeName": "Test Contant",
"firstName": "Test",
"lastName": "Contant",
"middleName": ""
},
"notes": "Guy yd he hcgd g\n",
"phones": [
{
"localizedLabel": "home",
"number": "054-8862488",
"originalLabel": "_$!<Home>!$_"
},
{
"localizedLabel": "iPhone",
"number": "054-7532635",
"originalLabel": "iPhone"
},
{
"localizedLabel": "work",
"number": "03-4214532",
"originalLabel": "_$!<Work>!$_"
}
],
"recordId": "908",
"source": {
"sourceID": 0,
"sourceType": ""
},
"websites": [
"www.layer.com",
"google.com"
]
}
]
数组位于变量$inputData
。
然后:
$decodedContact = json_decode($inputData['contact']); //disregard contact as it's coming from the body as key: contact and value: array`
我跑的时候:
foreach ($decodedContact as $contact){
echo $contact.'<br>';
}
我明白了:
Object of class stdClass could not be converted to string
当我var_dump($decodedData);
时,它看起来都很棒......:\
答案 0 :(得分:3)
如果contact
是stdClass
,则您无法echo
。
您可以在该对象上使用var_dump
(查看其值)或echo
值:
var_dump($contact);
或
echo $contact->birthday;
答案 1 :(得分:0)
$contact
变量解码后,是一个对象,你无法回应它!因为它不是一个字符串。
尝试var_dump($contact)
查看详细信息。
你可以访问它的属性,如:
$contact->adresses;
$contact->birthday
. . .
<强>更新强>
如果您想回复它,请尝试:
print_r($contact);