Although casting as an (object) works fine on a single hierarchical level, it doesn't go deep. In other words, if we want objects at every level, we'd have to do something like:
$foods = (object)[
"fruits" => (object)["apple" => 1, "banana" => 2, "cherry" => 3],
"vegetables" => (object)["asparagus" => 4, "broccoli" => 5, "carrot" => 6]
];
However, instead of doing multiple casting as objects, we can wrap the whole thing in a json_encode and json_decode like this:
$foods = json_decode(json_encode([
"fruits" => ["apple" => 1, "banana" => 2, "cherry" => 3],
"vegetables" => ["asparagus" => 4, "broccoli" => 5, "carrot" => 6]
]));
That makes sure it's an object at the deepest level.
To answer klewis, you can then access this like:
echo $foods->vegetables->broccoli;
This example would output the number 5.