2016-12-27 50 views
1

我試圖運行解碼陣列上一個foreach,具體如下:foreach所JSON是雖然沒有工作的var_dump正顯示出陣列

陣:

[ 
    { 
     "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": "[email protected]", 
       "localizedLabel": "Lab", 
       "originalLabel": "Lab" 
      }, 
      { 
       "address": "[email protected]", 
       "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": "[email protected]", 
       "localizedLabel": "Lab", 
       "originalLabel": "Lab" 
      }, 
      { 
       "address": "[email protected]", 
       "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": "[email protected]", 
       "localizedLabel": "Lab", 
       "originalLabel": "Lab" 
      }, 
      { 
       "address": "[email protected]", 
       "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": "[email protected]", 
       "localizedLabel": "Lab", 
       "originalLabel": "Lab" 
      }, 
      { 
       "address": "[email protected]", 
       "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": "[email protected]", 
       "localizedLabel": "Lab", 
       "originalLabel": "Lab" 
      }, 
      { 
       "address": "[email protected]", 
       "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": "[email protected]", 
       "localizedLabel": "Lab", 
       "originalLabel": "Lab" 
      }, 
      { 
       "address": "[email protected]", 
       "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

您是否嘗試過使用的var_dump($接觸 - >地址); ? – sAcH

回答

3

如果contactstdClass你不能echo它。 您可以在對象上使用var_dump(看到它的值)或echo值:

var_dump($contact); 

echo $contact->birthday; 
+0

Shalom Dekel,謝謝你的回覆。我實際上想要返回整個對象,所以我會得到聯繫,並且它都是節點子節點。 – DudeOfLayers

+0

問題是'echo'不能處理對象(因爲 - 你想如何顯示它們?)你將需要創建一個新的字符串來表示所有對象的子/屬性/節點/ etc。 – Dekel

+1

ba li lamut,它應該是print_r。謝謝 。 – DudeOfLayers

0

從JSON解碼$contact變量是一個對象後,你可以不回它!因爲它不是一個字符串。

嘗試var_dump($contact)以查看詳細信息。

您可以訪問它就像如下特性:

$contact->adresses; 
$contact->birthday 
. . . 

UPDATE:如果你想呼應這一切

嘗試:

print_r($contact);