Skip to content

Instantly share code, notes, and snippets.

@bobmagicii
Last active August 29, 2015 13:57
Show Gist options
  • Save bobmagicii/9756635 to your computer and use it in GitHub Desktop.
Save bobmagicii/9756635 to your computer and use it in GitHub Desktop.
<?php
// tested on php 5.4.4 on freebsd
// tested on php 5.5.9 on windows
// rename the error routes to e403 and e404, and this does not happen.
// -or-
// $array = get_object_vars($object->ErrorRoutes);
$json = <<< 'EOF'
{
"Namespaces": [
],
"Commonspace":"Routes\\Common",
"ErrorRoutes": {
"403": "Routes\\Error\\Forbidden",
"404": "Routes\\Error\\NotFound"
}
}
EOF;
// decode an object which has a property which is an object...
$object = json_decode($json);
var_dump($object);
// but we want that object to be an assoc array because those properties are
// not accessable without special syntax due to them starting with a number...
$array = (array)$object->ErrorRoutes;
var_dump($array);
// then we try to access them...
var_dump(
array_key_exists(404,$array),
array_key_exists("404",$array)
);
/*
C:\Users\bob\Desktop>php test.php
object(stdClass)#1 (3) {
["Namespaces"]=>
array(0) {
}
["Commonspace"]=>
string(13) "Routes\Common"
["ErrorRoutes"]=>
object(stdClass)#2 (2) {
["403"]=>
string(22) "Routes\Error\Forbidden"
["404"]=>
string(21) "Routes\Error\NotFound"
}
}
array(2) {
["403"]=>
string(22) "Routes\Error\Forbidden"
["404"]=>
string(21) "Routes\Error\NotFound"
}
bool(false)
bool(false)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment