-2
var_dump($img->files);

object(stdClass)#17 (6) {
  ["960"]=  string(46) "/imgs/14c99db10ee9cc28bfa7db16e961fd65-960.jpg"
  ["60"]=   string(45) "/imgs/14c99db10ee9cc28bfa7db16e961fd65-60.jpg"
  ["100"]=  string(46) "/imgs/14c99db10ee9cc28bfa7db16e961fd65-100.jpg"
  ["200"]=  string(46) "/imgs/14c99db10ee9cc28bfa7db16e961fd65-200.jpg"
  ["300"]=  string(46) "/imgs/14c99db10ee9cc28bfa7db16e961fd65-300.jpg"
  ["500"]=  string(46) "/imgs/14c99db10ee9cc28bfa7db16e961fd65-500.jpg"
}

how can use one index

var_dump($img->files["60"]);

Fails

Fatal error: Cannot use object of type stdClass as array

7
  • Have you tried $img->files->{"60"} ? (forgive me if the syntax is slightly off, been a while for PHP Commented Jun 23, 2012 at 21:37
  • 1
    possible duplicate of Casting an array with numeric keys as an object Commented Jun 23, 2012 at 21:39
  • 1
    Note to voters for "this is not a real question": this is a real question. It is a very unexpected and undocumented flaw in PHP. It is a duplicate question though. Commented Jun 23, 2012 at 21:45
  • Yes, surely a duplicate. But I agree that this is THE gotcha of PHP. Commented Jun 23, 2012 at 21:46
  • 1
    Ah, turned out this is actually one of those "fixed, now documentation notes the buggy behavior and it is officially the correct behavior" bugs in the tracker. Commented Jun 23, 2012 at 21:59

3 Answers 3

2

It says right there in your var_dump that $img->files is not an array, it is an object of class stdClass.

You should access it using object syntax.

$img->files->{'60'};
Sign up to request clarification or add additional context in comments.

12 Comments

Yes, I did. I don't have the code that generates his $img object or his $img->files object. But i generated some objects like that myself and it worked by accessing via the arrow operator.
Also tried it and got Undefined property: stdClass::$60, seems like one of PHP's unexplained weidnesses.
Of course. ) You cannot access a numeric property directly.
And I really wonder who upvoted this. )
Seems like one of those "features" that everybody sees as a bug, that get the "thank you for your report but this is not a bug" in the PHP bugtracker.
|
1

That's how we can get the data structure similar to what you got:

$img = new stdClass();
$img->files = array(960 => "/imgs/14c99db10ee9cc28bfa7db16e961fd65-960.jpg");
$img->files = (object)$img->files;

... then this:

var_dump($img->files->{'960'}); // NULL

... will just print NULL. But this...

$img->files = (array)$img->files;
var_dump($img->files[960]);

... will work. ) You need to cast an array-like object (which was obviously casted to object by some external code by error) back to array. Then just access its elements as usual.

Fiddle to play with.

UPDATE: ... and here's a link to the post which explains the reason of this behavior much better than I'll ever be able to. )

Comments

0

PHP 5.3.10, running this test:

$obj = (object)array(10 => 'foo');
$obj->{20} = 'bar';

var_dump($obj);

print($obj->{10}."\n");
print($obj->{20}."\n");

Running it I get this result:

object(stdClass)#1 (2) {
  [10]=>
  string(3) "foo"
  ["20"]=>
  string(3) "bar"
}
PHP Notice:  Undefined property: stdClass::$10 in /home/lanzz/tmp/test.php on line 8

bar

So, numeric properties cast from array get assigned to actual integer properties in the object which are inaccessible, while numeric properties assigned to the object get string properties, which are accessible.

I vote it's just another bug in PHP.

1 Comment

I'd like that abbreviated, like YAPHPB. )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.