简体   繁体   中英

How to access sub array in JSON - PHP

I have posted parts of this.. but this a different question for it

i have the below

foreach ($results['comments'] as $item) {

  echo 'Date: '. $item['created_at'] .'<br/>';
  echo 'Description : '. $item['html_body'] .'<br/>';
  echo 'Attachments : '. $item['attacments->url'] .'<br/>';
  echo 'Filename : '. $item['file_name'] .'<br/>';
  echo "<br>";
}

So basically, my Date and Description work, BUT the attachments wont work, b/ci dont think thats the correct way to get an object thats within an array of an array? hope i explained it correctly.

the comments array has all the date as a single object and so is description, then it has this trailing.

[public] => 1 [trusted] => 1 [attachments] => Array ( [0] => Array ( [url] => https://url/api/v2/attachments/IDHERE.json [id] => ID#[file_name] => name of file here

Take a look at your array dump

[public] => 1
[trusted] => 1
[attachments] => Array (
    [0] => Array (
        [url] => https://url/api/v2/attachments/IDHERE.json
        [id] => ID#
        [file_name] => name of file here

Get the values like this:

$Attachments = $item['attachments'];
$AttachmentsUrl = $Attachments[0]['url'];
$Attachmentsid = $Attachments[0]['id'];
$AttachmentsFileName = $Attachments[0]['file_name'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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