5

print_r($p->attachments) produces:

Array
(
    [0] => stdClass Object
        (
            [id] => ...
            [url] => http://...png
            [slug] => ...
            [title] => ...
            [description] => ...
            [caption] => ...
            [parent] => ...
            [mime_type] => image/png
            [images] => ...
                (
                )
        )
)

I wish to access the value in the url field

print_r($p->attachments[0]->url) retrieves the url, but also produces: Undefined offset: 0

Now I can supress the error by calling print_r(@$p->attachments[0]->url), but is there a proper way of fixing this?

I am not able to modify the $p object.

Edit:

As suggested, here is response from Var_dump($p->attachments)

 array(1) {
  [0]=>
  object(stdClass)#322 (9) {
    ["id"]=>
    int(1814)
    ["url"]=>
    string(76) "..."
    ["slug"]=>
    string(34) "..."
    ["title"]=>
    string(34) "..."
    ["description"]=>
    string(0) ""
    ["caption"]=>
    string(53) "..."
    ["parent"]=>
    int(1811)
    ["mime_type"]=>
    string(9) "image/png"
    ["images"]=>
    array(0) {
    }
  }
}
Gravy
  • 12,264
  • 26
  • 124
  • 193
  • Use `@print_r($p->attachments[0]->url)` – DontVoteMeDown Sep 18 '13 at 19:15
  • 1
    Try var_dump()ing the object -- the value might not be set – Sterling Archer Sep 18 '13 at 19:16
  • 5
    @DontVoteMeDown I would if I could. That buries the problem rather than solves it. – George Brighton Sep 18 '13 at 19:17
  • 3
    it seems that `$p->attachments[0]` is not defined every time you try to access it. You can suppress errors if you do: `if(isset($p->attachments[0])) { .... }` before trying to access it. – steven Sep 18 '13 at 19:17
  • From the code you've given, there's no reason for the error. I image `$p->attachments` is sometimes empty, in which case you need to check for this before accessing its first element. – George Brighton Sep 18 '13 at 19:19
  • @steven that doesn't guarantee that `url` is set. Also, should use `array_key_exists` for checking if a key in an array/hashtable exists. – crush Sep 18 '13 at 19:19
  • @crush not necessarily - http://stackoverflow.com/questions/3210935 – George Brighton Sep 18 '13 at 19:22
  • @GeorgeBrighton Interesting. I guess I didn't consider an array index with a null value. I guess that's because I don't code in a manner that allows that to happen, but I see some say that `isset` is faster than `array_key_exists` which is interesting as well. – crush Sep 18 '13 at 19:23
  • @crush I usually don't care about such minor performance issues. If I don't have to allow *null* values, I always go with `isset()`, just because it is a shorter name. – ComFreek Sep 18 '13 at 19:27
  • I never used `array_key_exists`. @crush It would be good defensive practice for you to use `isset` - if you do get a null, you'll know much sooner. – George Brighton Sep 18 '13 at 19:29
  • @GeorgeBrighton `array_key_exists` and what about cases where it is desirable to have a `null` value in a map? It's pretty common structure to allow `NULL` values in maps in order to preserve the keys. I'd say most of the time, though, given this new knowledge, `isset` is the best choice. – crush Sep 18 '13 at 19:30
  • Search for an answer before asking! This question has probably been asked every single day for a few years now. – Sverri M. Olsen Sep 18 '13 at 19:53
  • Bit harsh mr power trip... tried solving problem myself, couldn't see anything wrong with my code searched and found lots of not-proper ways of doing things, still don't fully understand why i need to check if it is set to initialise the array if the data is already in there... so I don't think that was cause to rate question down @SverriM.Olsen. Thanks to all of those of you who actually had something constructive to say and help me. I don't even know if the solution is the proper solution... surpressing errors before accessing??? – Gravy Sep 19 '13 at 00:04
  • @GeorgeBrighton just kidding man.. – DontVoteMeDown Sep 19 '13 at 12:15
  • @Gravy I stand behind my comment. This question has been [asked](http://stackoverflow.com/questions/16936544/undefined-variable-and-offset) [every](http://stackoverflow.com/questions/5424236/undefined-offset-1) [single](http://stackoverflow.com/questions/9399509/getting-rid-of-php-notice-undefined-offset) [day](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) [for](http://stackoverflow.com/questions/17297315/undefined-offset) [years](http://stackoverflow.com/questions/3209386/undefined-offset-error-but-offset-is-not-undefined). – Sverri M. Olsen Sep 19 '13 at 13:31

2 Answers2

15

You can use isset() for check the array:

if(isset($p->attachments[0])){
    echo $p->attachments[0]->url;
}
else {
  //some error?
}

Or if you know that you are only going to be checking index 0 then you can do this way

$array = $array + array(null);

So if the original $array[0] was unset, now it is null

Suvash sarker
  • 3,140
  • 1
  • 18
  • 21
0

dunno why it returns you a value and informs you that there is undefined offset 0. strange.

can you check this (since attachments is an array you should do this that way):

if ( count($p->attachments) > 0 ){ 
   foreach ($p->attachments AS $att){
    //do the stuff if attachement 
   }
}
halfik
  • 34
  • 2