1

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/c i 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
2
  • try $item['attachments'][0]['url'] Commented Feb 6, 2014 at 16:41
  • How about file_name? would it be $item['attachments'][0]['url'][0]['id'][0]['file_name'] @Nouphal.M Commented Feb 6, 2014 at 16:47

1 Answer 1

2

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'];
Sign up to request clarification or add additional context in comments.

4 Comments

can i ask, since the code is giving me an error of PHP Notice: Undefined offset: 0, can't i use the name of the array instead? how would that look @meda
@user3100345 if it throws an error, it means $Attachments[0] does not have any element at that index
Should i put an if statement?
@user3100345 yes check if attachment array is set, maybe not all you comments have attachment, see this answer coincidentally it simillar to your case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.