2

I got an object's value in foreach loop which is a number

> array(12) { [0]=> object(stdClass)#34 (22) {
                    [201609]=> string(6) "130000"
                  } }

When I try to access $query->201609 getting error

Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'

I try to access by using $query->{201609} it gives me 0 value instead of 130000

How can I get that attribute?

This question is not duplicate. I've tried the other question's it doesn't help for me because it is in a loop.

2 Answers2

0

Try

$query->{'201609'}

Seems to be duplicate of Get a PHP object property that is a number

Damian Dziaduch
  • 2,107
  • 1
  • 15
  • 16
  • I tried this it gives me 0 value instead of 130000. Which I was said in my question. –  May 16 '18 at 05:36
0

This works in Php 7.2.4 (likely older versions as well):

$property = 201609;
var_dump($query->$property);

Tested like this:

$x = new stdClass();
$p = 123;
$x->$p = 456;
var_dump($x);

Output:

object(stdClass)#3 (1) {
  ["123"]=>
  int(456)
}

Assuming "$query" is an array, not the object in question:

$property = 201609;
var_dump($query[0]->$property);