There a lots of questions/answers here on SO about multidim arrays and foreach loops but I haven't found one that applies specifically to my case. Please advise if you think otherwise.
My array is structured as below, and is output to a CodeIgniter view:
Array
(
[2] => Array //this is the unique user ID
(
[name] => Joe Schmoe
[cars] => Array
(
[112] => Array //this is the unique car ID
(
[cars_name] => Honda
[cars_type] => Sedan
[cars_color] => White
)
[102] => Array
(
[cars_name] => Toyota
[cars_type] => Sedan
[cars_color] => Black
)
[113] => Array
(
[cars_name] => Nissan
[cars_type] => Coupe
[cars_color] => Red
)
)
)
[5] => Array
(
[name] => Buck
[cars] => Array
(
[147] => Array
(
[cars_name] => Tesla
[cars_type] => Sedan
[cars_color] => Yellow
)
)
)
[1] => Array
(
[name] => Mike Mechanic
[cars] => Array
(
[140] => Array
(
[cars_name] => BMW
[cars_type] => SUV
[cars_color] => Blue
)
[145] => Array
(
[cars_name] => MB
[cars_type] => Sedan
[cars_color] => Gray
)
)
)
)
I need help building the nested foreach
loops so I can have access to each one of the elements in the entire array.
I tried this nested foreach
structure but it's a no go.
foreach ($results as $data):
if (is_array($data)):
foreach ($data as $value):
if (is_array($value)):
foreach ($value as $row):
endforeach;
endif;
endforeach;
endif;
endforeach;
echo $row->cars_name //returns a non-object error
Anyone know how to put this nested loop together?
Thanks for helping, much appreciated.
Trying to get property of non-object
error when trying to echo an array element – torr May 22 '11 at 15:56