So I read that to convert an array to an object, I can simply use (object)
in front of the variable like $my_obj = (object) $my_array
. With that said, Why do I get the following error when executing the code below.
NOTICE Trying to get property of non-object on line number 26
Shouldn't I be able to access the object's properties by using $car->make
?
<?php
$cars = array(
array(
'make' => 'Audi',
'model' => 'A4',
'year' => '2014',
),
array(
'make' => 'Benz',
'model' => 'c300',
'year' => '2015',
),
array(
'make' => 'BMW',
'model' => 'i8',
'year' => '2016',
),
);
// Convert $cars array to object
$cars = (object) $cars;
foreach ($cars as $car) {
// Shouldn't I be able to access my object
print $car->make . "<br>";
}