PHP - how to cast multidimensional object? - php

I am wondering if there is a built-in way in PHP to cast multidimensional objects to arrays?
The problem is when applying a regular casting on an object, the first dimension only is being affected, all other dimensions remind the same.
Note: I am interested in casting only!
Example:
$a = new stdClass();
$a->b = 'qwe';
$a->c = new stdClass();
$a->c->d = 'asd';
var_dump((array)$a); // echoes:
array(2) {
["b"]=>
string(3) "qwe"
["c"]=>
object(stdClass)#2 (1) {
["d"]=>
string(3) "asd"
}
}
As you can see only the first dimension was affected, so how to cast multidimensional objects?

There is no official way to cast a multi-level object to an array but the good news is that there is a hack.
Use json_encode() to get a JSON representation of your object then pass the result to json_decode() and use TRUE as its second argument to get arrays instead of objects.
$a = new stdClass();
$a->b = 'qwe';
$a->c = new stdClass();
$a->c->d = 'asd';
print_r(json_decode(json_encode($a), TRUE));
The output is:
Array
(
[b] => qwe
[c] => Array
(
[d] => asd
)
)
The method has some drawbacks (it cannot handle resources, for example) but they are just minor annoyances.

Since your question is if it's possible using only a single built-in PHP function to recursively cast object and children objects as array, without even any user-made callback function, the answer is no, it can't be done like that.
There are other ways to achieve it, though.

Related

addressing an array with a "string integer" as a key PHP [duplicate]

I am using json_decode() something like:
$myVar = json_decode($data)
Which gives me output like this:
[highlighting] => stdClass Object
(
[448364] => stdClass Object
(
[Data] => Array
(
[0] => Tax amount liability is .......
I want to access the string value in the key [0]. When I try to do something like:
print $myVar->highlighting->448364->Data->0;
I get this error:
Parse error: syntax error, unexpected T_DNUMBER
The two numerals/integers there seems to be problem.
Updated for PHP 7.2
PHP 7.2 introduced a behavioral change to converting numeric keys in object and array casts, which fixes this particular inconsistency and makes all the following examples behave as expected.
One less thing to be confused about!
Original answer (applies to versions earlier than 7.2.0)
PHP has its share of dark alleys that you really don't want to find yourself inside. Object properties with names that are numbers is one of them...
What they never told you
Fact #1: You cannot access properties with names that are not legal variable names easily
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->123foo; // error
Fact #2: You can access such properties with curly brace syntax
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
Fact #3: But not if the property name is all digits!
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
echo $o->{'123'}; // error!
Live example.
Fact #4: Well, unless the object didn't come from an array in the first place.
$a = array('123' => '123');
$o1 = (object)$a;
$o2 = new stdClass;
$o2->{'123'} = '123'; // setting property is OK
echo $o1->{'123'}; // error!
echo $o2->{'123'}; // works... WTF?
Live example.
Pretty intuitive, don't you agree?
What you can do
Option #1: do it manually
The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties:
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
$a = (array)$o;
echo $o->{'123'}; // error!
echo $a['123']; // OK!
Unfortunately, this does not work recursively. So in your case you 'd need to do something like:
$highlighting = (array)$myVar->highlighting;
$data = (array)$highlighting['448364']->Data;
$value = $data['0']; // at last!
Option #2: the nuclear option
An alternative approach would be to write a function that converts objects to arrays recursively:
function recursive_cast_to_array($o) {
$a = (array)$o;
foreach ($a as &$value) {
if (is_object($value)) {
$value = recursive_cast_to_array($value);
}
}
return $a;
}
$arr = recursive_cast_to_array($myVar);
$value = $arr['highlighting']['448364']['Data']['0'];
However, I 'm not convinced that this is a better option across the board because it will needlessly cast to arrays all of the properties that you are not interested in as well as those you are.
Option #3: playing it clever
An alternative of the previous option is to use the built-in JSON functions:
$arr = json_decode(json_encode($myVar), true);
$value = $arr['highlighting']['448364']['Data']['0'];
The JSON functions helpfully perform a recursive conversion to array without the need to define any external functions. However desirable this looks, it has the "nuke" disadvantage of option #2 and additionally the disadvantage that if there is any strings inside your object, those strings must be encoded in UTF-8 (this is a requirement of json_encode).
Just wanted to add to Jon's eloquent explanation the reason why this fails. It's all because when creating an array, php converts keys to integers — if it can — which causes lookup problems on arrays which have been cast to objects, simply because the numeric key is preserved. This is problematic because all property access options expect or convert to strings. You can confirm this by doing the following:
$arr = array('123' => 'abc');
$obj = (object) $arr;
$obj->{'123'} = 'abc';
print_r( $obj );
Which would output:
stdClass Object (
[123] => 'abc',
[123] => 'abc'
)
So the object has two property keys, one numeric (which can't be accessed) and one string based. This is the reason why Jon's #Fact 4 works, because by setting the property using curly braces means you always define a string-based key, rather than numeric.
Taking Jon's solution, but turning it on its head, you can generate an object from your array that always has string-based keys by doing the following:
$obj = json_decode(json_encode($arr));
From now on you can use either of the following because access in this manner always converts the value inside the curly brace to a string:
$obj->{123};
$obj->{'123'};
Good old illogical PHP...
For PHP 7
Accessing Object properties having numbers as property name.
Mostly needed after casting array to object.
$arr = [2,3,7];
$o = (object) $arr;
$t = "1";
$t2 = 1;
$t3 = (1);
echo $o->{1}; // 3
echo $o->{'1'}; // 3
echo $o->$t; // 3
echo $o->$t2; // 3
echo $o->$t3; // 3
echo $o->1; // error
echo $o->(1); // error
If an object begins with # like:
SimpleXMLElement Object (
[#attributes] => Array (
[href] => qwertyuiop.html
[id] => html21
[media-type] => application/xhtml+xml
)
)
You have to use:
print_r($parent_object->attributes());
because $parent_object->{'#attributes'} or $parent_object['#attributes'] won't work.
A final alternative to Jon's comprehensive answer:
Simply use json_decode() with the second parameter set to true.
$array = json_decode($url, true);
This then returns an associative array rather than an object so no need to convert after the fact.
This may not be suitable to every application but it really helped me to easily reference a property of the oroginal object.
Solution was found in this tutorial - http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP/
Regards
I had copied this function from the net. If it works as it says ("Function to Convert stdClass Objects to Multidimensional Arrays"), try the following:
<?php
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
?>
first pass your array to objectToArray function
then take the return value
echo [highlighting][448364][Data][0]
Source: PHP stdClass to Array and Array to stdClass
I'm afraid you aren't allowed to name objects starting with numerics. Rename the first one "448364" starting with a letter.
The second one is an array, those are to be accessed by brackets like so:
print myVar->highlighting->test_448364->Data[0]
instead

Working with PHP objects that contain numerical property names [duplicate]

I am using json_decode() something like:
$myVar = json_decode($data)
Which gives me output like this:
[highlighting] => stdClass Object
(
[448364] => stdClass Object
(
[Data] => Array
(
[0] => Tax amount liability is .......
I want to access the string value in the key [0]. When I try to do something like:
print $myVar->highlighting->448364->Data->0;
I get this error:
Parse error: syntax error, unexpected T_DNUMBER
The two numerals/integers there seems to be problem.
Updated for PHP 7.2
PHP 7.2 introduced a behavioral change to converting numeric keys in object and array casts, which fixes this particular inconsistency and makes all the following examples behave as expected.
One less thing to be confused about!
Original answer (applies to versions earlier than 7.2.0)
PHP has its share of dark alleys that you really don't want to find yourself inside. Object properties with names that are numbers is one of them...
What they never told you
Fact #1: You cannot access properties with names that are not legal variable names easily
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->123foo; // error
Fact #2: You can access such properties with curly brace syntax
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
Fact #3: But not if the property name is all digits!
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
echo $o->{'123'}; // error!
Live example.
Fact #4: Well, unless the object didn't come from an array in the first place.
$a = array('123' => '123');
$o1 = (object)$a;
$o2 = new stdClass;
$o2->{'123'} = '123'; // setting property is OK
echo $o1->{'123'}; // error!
echo $o2->{'123'}; // works... WTF?
Live example.
Pretty intuitive, don't you agree?
What you can do
Option #1: do it manually
The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties:
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
$a = (array)$o;
echo $o->{'123'}; // error!
echo $a['123']; // OK!
Unfortunately, this does not work recursively. So in your case you 'd need to do something like:
$highlighting = (array)$myVar->highlighting;
$data = (array)$highlighting['448364']->Data;
$value = $data['0']; // at last!
Option #2: the nuclear option
An alternative approach would be to write a function that converts objects to arrays recursively:
function recursive_cast_to_array($o) {
$a = (array)$o;
foreach ($a as &$value) {
if (is_object($value)) {
$value = recursive_cast_to_array($value);
}
}
return $a;
}
$arr = recursive_cast_to_array($myVar);
$value = $arr['highlighting']['448364']['Data']['0'];
However, I 'm not convinced that this is a better option across the board because it will needlessly cast to arrays all of the properties that you are not interested in as well as those you are.
Option #3: playing it clever
An alternative of the previous option is to use the built-in JSON functions:
$arr = json_decode(json_encode($myVar), true);
$value = $arr['highlighting']['448364']['Data']['0'];
The JSON functions helpfully perform a recursive conversion to array without the need to define any external functions. However desirable this looks, it has the "nuke" disadvantage of option #2 and additionally the disadvantage that if there is any strings inside your object, those strings must be encoded in UTF-8 (this is a requirement of json_encode).
Just wanted to add to Jon's eloquent explanation the reason why this fails. It's all because when creating an array, php converts keys to integers — if it can — which causes lookup problems on arrays which have been cast to objects, simply because the numeric key is preserved. This is problematic because all property access options expect or convert to strings. You can confirm this by doing the following:
$arr = array('123' => 'abc');
$obj = (object) $arr;
$obj->{'123'} = 'abc';
print_r( $obj );
Which would output:
stdClass Object (
[123] => 'abc',
[123] => 'abc'
)
So the object has two property keys, one numeric (which can't be accessed) and one string based. This is the reason why Jon's #Fact 4 works, because by setting the property using curly braces means you always define a string-based key, rather than numeric.
Taking Jon's solution, but turning it on its head, you can generate an object from your array that always has string-based keys by doing the following:
$obj = json_decode(json_encode($arr));
From now on you can use either of the following because access in this manner always converts the value inside the curly brace to a string:
$obj->{123};
$obj->{'123'};
Good old illogical PHP...
For PHP 7
Accessing Object properties having numbers as property name.
Mostly needed after casting array to object.
$arr = [2,3,7];
$o = (object) $arr;
$t = "1";
$t2 = 1;
$t3 = (1);
echo $o->{1}; // 3
echo $o->{'1'}; // 3
echo $o->$t; // 3
echo $o->$t2; // 3
echo $o->$t3; // 3
echo $o->1; // error
echo $o->(1); // error
If an object begins with # like:
SimpleXMLElement Object (
[#attributes] => Array (
[href] => qwertyuiop.html
[id] => html21
[media-type] => application/xhtml+xml
)
)
You have to use:
print_r($parent_object->attributes());
because $parent_object->{'#attributes'} or $parent_object['#attributes'] won't work.
A final alternative to Jon's comprehensive answer:
Simply use json_decode() with the second parameter set to true.
$array = json_decode($url, true);
This then returns an associative array rather than an object so no need to convert after the fact.
This may not be suitable to every application but it really helped me to easily reference a property of the oroginal object.
Solution was found in this tutorial - http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP/
Regards
I had copied this function from the net. If it works as it says ("Function to Convert stdClass Objects to Multidimensional Arrays"), try the following:
<?php
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
?>
first pass your array to objectToArray function
then take the return value
echo [highlighting][448364][Data][0]
Source: PHP stdClass to Array and Array to stdClass
I'm afraid you aren't allowed to name objects starting with numerics. Rename the first one "448364" starting with a letter.
The second one is an array, those are to be accessed by brackets like so:
print myVar->highlighting->test_448364->Data[0]
instead

Access array element indexed by numerical string

I have encountered something odd.
I have a php array, indexed with numerical keys.
However it appears impossible to access any of the elements because php automatically treats numerical strings as integers, causing an illegal offset notice.
Under normal circumstances its imposable to create a php array with numerical string indexes, but it can happen with type casting.
To reproduce:
$object = new stdClass();
$object->{'1'} = 'one';
$array = (array) $object;
var_dump($array);
/* produces
array(1) {
["1"]=>
string(3) "one"
}
*/
//none of the following will work
$key = '1';
echo $array[1], $array['1'], $array["1"], $array[(string)1], $array[$key];
Is this just an edge case bug? I only encountered the problem when attempting to improve my answer for another SO question
Live code example: http://codepad.viper-7.com/dFSlH1
Unbelievable but this is normal behavior in php, it was considered as a bug (link) in the year 2008.
But they just pointed out to the manual for the cast with (array):
If an object is converted to an array, the result is an array whose
elements are the object's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are
unaccessible;
You can use get_object_vars() instead:
$object = new stdClass();
$object->{'1'} = 'one';
$array = get_object_vars( $object );
$key = '1';
echo $array[1]."<br>";
echo $array['1']."<br>";
echo $array["1"]."<br>";
echo $array[(string)1]."<br>";
echo $array[$key]."<br>";
Doesn't explain why this happens, but is a solution to avoid the cast problem.
Off topic but I thought maybe it is interesting. Found this in the manual.
To avoid these kind of problems, always use an integer OR a string as index, don't mix it up and don't use integers in a string.
Example of mixed array:
$array = array(
1 => "a",
"1" => "b",//overrides 1
1.5 => "c",//overrides "1"
true => "d",//overrides 1.5
);
var_dump($array);
You can use
$vars = get_object_vars($object);
echo $vars[1];
String keys containing valid integer values would be cast to integer keys automatically in “normal” array creation – but it seems casting from object to array doesn’t apply the same logic.
It can be fixed however, by using
$array = array_combine(array_keys($array), array_values($array));
after your line that creates the array from the object. http://codepad.viper-7.com/v5rGJa
Although, same as Dave already said in his comment, using get_object_vars looks like a “cleaner” solution to me as well.
foreach ($array as $key => $value){
var_dump($key);
var_dump($value);
}
shows
string(1) "1"
string(3) "one"
But echo $array['"1"']; gives
E_NOTICE : type 8 -- Undefined index: "1" -- at line 8
That's strange!

Accessing "0" inside SimpleXMLObject in PHP [duplicate]

I am using json_decode() something like:
$myVar = json_decode($data)
Which gives me output like this:
[highlighting] => stdClass Object
(
[448364] => stdClass Object
(
[Data] => Array
(
[0] => Tax amount liability is .......
I want to access the string value in the key [0]. When I try to do something like:
print $myVar->highlighting->448364->Data->0;
I get this error:
Parse error: syntax error, unexpected T_DNUMBER
The two numerals/integers there seems to be problem.
Updated for PHP 7.2
PHP 7.2 introduced a behavioral change to converting numeric keys in object and array casts, which fixes this particular inconsistency and makes all the following examples behave as expected.
One less thing to be confused about!
Original answer (applies to versions earlier than 7.2.0)
PHP has its share of dark alleys that you really don't want to find yourself inside. Object properties with names that are numbers is one of them...
What they never told you
Fact #1: You cannot access properties with names that are not legal variable names easily
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->123foo; // error
Fact #2: You can access such properties with curly brace syntax
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
Fact #3: But not if the property name is all digits!
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
echo $o->{'123'}; // error!
Live example.
Fact #4: Well, unless the object didn't come from an array in the first place.
$a = array('123' => '123');
$o1 = (object)$a;
$o2 = new stdClass;
$o2->{'123'} = '123'; // setting property is OK
echo $o1->{'123'}; // error!
echo $o2->{'123'}; // works... WTF?
Live example.
Pretty intuitive, don't you agree?
What you can do
Option #1: do it manually
The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties:
$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
$a = (array)$o;
echo $o->{'123'}; // error!
echo $a['123']; // OK!
Unfortunately, this does not work recursively. So in your case you 'd need to do something like:
$highlighting = (array)$myVar->highlighting;
$data = (array)$highlighting['448364']->Data;
$value = $data['0']; // at last!
Option #2: the nuclear option
An alternative approach would be to write a function that converts objects to arrays recursively:
function recursive_cast_to_array($o) {
$a = (array)$o;
foreach ($a as &$value) {
if (is_object($value)) {
$value = recursive_cast_to_array($value);
}
}
return $a;
}
$arr = recursive_cast_to_array($myVar);
$value = $arr['highlighting']['448364']['Data']['0'];
However, I 'm not convinced that this is a better option across the board because it will needlessly cast to arrays all of the properties that you are not interested in as well as those you are.
Option #3: playing it clever
An alternative of the previous option is to use the built-in JSON functions:
$arr = json_decode(json_encode($myVar), true);
$value = $arr['highlighting']['448364']['Data']['0'];
The JSON functions helpfully perform a recursive conversion to array without the need to define any external functions. However desirable this looks, it has the "nuke" disadvantage of option #2 and additionally the disadvantage that if there is any strings inside your object, those strings must be encoded in UTF-8 (this is a requirement of json_encode).
Just wanted to add to Jon's eloquent explanation the reason why this fails. It's all because when creating an array, php converts keys to integers — if it can — which causes lookup problems on arrays which have been cast to objects, simply because the numeric key is preserved. This is problematic because all property access options expect or convert to strings. You can confirm this by doing the following:
$arr = array('123' => 'abc');
$obj = (object) $arr;
$obj->{'123'} = 'abc';
print_r( $obj );
Which would output:
stdClass Object (
[123] => 'abc',
[123] => 'abc'
)
So the object has two property keys, one numeric (which can't be accessed) and one string based. This is the reason why Jon's #Fact 4 works, because by setting the property using curly braces means you always define a string-based key, rather than numeric.
Taking Jon's solution, but turning it on its head, you can generate an object from your array that always has string-based keys by doing the following:
$obj = json_decode(json_encode($arr));
From now on you can use either of the following because access in this manner always converts the value inside the curly brace to a string:
$obj->{123};
$obj->{'123'};
Good old illogical PHP...
For PHP 7
Accessing Object properties having numbers as property name.
Mostly needed after casting array to object.
$arr = [2,3,7];
$o = (object) $arr;
$t = "1";
$t2 = 1;
$t3 = (1);
echo $o->{1}; // 3
echo $o->{'1'}; // 3
echo $o->$t; // 3
echo $o->$t2; // 3
echo $o->$t3; // 3
echo $o->1; // error
echo $o->(1); // error
If an object begins with # like:
SimpleXMLElement Object (
[#attributes] => Array (
[href] => qwertyuiop.html
[id] => html21
[media-type] => application/xhtml+xml
)
)
You have to use:
print_r($parent_object->attributes());
because $parent_object->{'#attributes'} or $parent_object['#attributes'] won't work.
A final alternative to Jon's comprehensive answer:
Simply use json_decode() with the second parameter set to true.
$array = json_decode($url, true);
This then returns an associative array rather than an object so no need to convert after the fact.
This may not be suitable to every application but it really helped me to easily reference a property of the oroginal object.
Solution was found in this tutorial - http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP/
Regards
I had copied this function from the net. If it works as it says ("Function to Convert stdClass Objects to Multidimensional Arrays"), try the following:
<?php
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
?>
first pass your array to objectToArray function
then take the return value
echo [highlighting][448364][Data][0]
Source: PHP stdClass to Array and Array to stdClass
I'm afraid you aren't allowed to name objects starting with numerics. Rename the first one "448364" starting with a letter.
The second one is an array, those are to be accessed by brackets like so:
print myVar->highlighting->test_448364->Data[0]
instead

Decode sparse json object to php array

I can create a sparse php array (or map) using the command:
$myarray = array(10=>'hi','test20'=>'howdy');
I want to serialize/deserialize this as JSON. I can serialize it using the command:
$json = json_encode($myarray);
which results in the string {"10":"hi","test20":"howdy"}. However, when I deserialize this and cast it to an array using the command:
$mynewarray = (array)json_decode($json);
I seem to lose any mappings with keys which were not valid php identifiers. That is, mynewarray has mapping 'test20'=>'howdy', but not 10=>'hi' nor '10'=>'hi'.
Is there a way to preserve the numerical keys in a php map when converting to and back from json using the standard json_encode / json_decode functions?
(I am using PHP Version 5.2.10-2ubuntu6.4.)
json_decode returns an object of type stdClass by default. You access members as properties (i.e., $result->test20). 10 isn't a valid name for a property, which is why you're losing it.
Instead of casting to an array, you can pass true as a second argument to json_decode to make it return an associative array itself:
$mynewarray = json_decode($json, true);
If you do that, $mynewarray[10] will work fine.
What version of PHP? On 5.2 the following program/script
$myarray = array(10=>'hi','test20'=>'howdy');
$json = json_encode($myarray);
$mynewarray = (array) json_decode($json);
var_dump($mynewarray);
Outputs
array(2) {
["10"]=>
string(2) "hi"
["test20"]=>
string(5) "howdy"
}
Which doesn't display the behavior you're describing.
That said, if your version of PHP is miscasting the JSON, try using get_object_vars on the stdClass object that json_decode returns
get_object_vars(json_decode($json))
That might return better results.
The problem is in the conversion from object to array.
$a = (array)json_decode('{"10":"hi","test20":"howdy"}');
var_dump($a);
//outputs
array(2) {
["10"]=>
string(2) "hi"
["test20"]=>
string(5) "howdy"
}
See how this array have index "10"? But in PHP, everything that looks like a number gets converted into a number, especially in array indexes. You can't just get a["10"] because it converts "10" into a number and this array does not have such an index.
However, foreach works.
foreach ($a as $key => $value) {
var_dump($key);
var_dump($value);
}
//outputs
string(2) "10"
string(2) "hi"
string(6) "test20"
string(5) "howdy"
You can also treat result of json_decode as an object. While you won't be able to do $a->10 or $a->"10",
$a = json_decode('{"10":"hi","test20":"howdy"}');
$b = 10;
var_dump($a->$b);
//outputs
string(2) "hi"
works.
But most likely, as Chris said, you just want to pass true as a second argument.
$a = json_decode('{"10":"hi","test20":"howdy"}', true);
var_dump($a[10]);
//outputs
string(2) "hi"

Categories