Yes, this is possible using the cast array of the stdClass object:
$data = new stdClass; $data->{"12"} = 37; $data = (array) $data; var_dump( $data );
This gives you (prior to PHP 7.1):
array(1) { ["12"]=> int(37) }
(Update: my original answer showed a more complex way using json_decode() and json_encode() , which is not needed.)
Pay attention to the comment . Unfortunately, it is not possible to directly refer to the value: $data['12'] will result in a notification.
Update :
From PHP 7.2, you can also use a numeric string as a key to refer to a value on it:
var_dump( $data['12'] ); // int 32
David 03 Feb '16 at 15:03 2016-02-03 15:03
source share