I have an array of PHP objects (lets say orders) which have arrays of items like so (JSON for ease of notation):
$orders = [
{
id: 87,
user_id: 6,
menu_id: 9,
items: [
{
id: 1,
name: "Item 1",
}
]
},
{
id: 88,
user_id: 3,
menu_id: 6,
items: [
{
id: 2,
name: "Item 2",
}
]
}
]
I'm trying to make a CSV row with titles of this kind of array. I'm using a keyed array like so:
$settings = [
'Order ID' => 'id',
'User ID' => 'user_id'
];
So now I can go...
$csv = [];
$csv[] = array_keys($settings);
foreach($orders as $order):
$csv_row = [];
foreach($settings as $column);
$csv_row[] = $order->{$column};
endforeach;
$csv[] = $csv_row;
endforeach;
This gives me:
[ 'Order ID', 'User ID' ]
[ '87', '6' ]
[ '88', '3' ]
Is there any way I can dynamically access the 'deeper' variables? i.e. $order->items[0]->name, in a similar way to the curly bracket notation? I need to do it dynamically as there are lots of different object and relations that I need to export as CSV files.
P.S. Its really late – sorry if this is a crazy question
$order[0]['name']? You can't build that kind of array "path" dynamically without resorting to evil stuff likeeval, but if you know how "deep" into the array you have to go, you can easy do$order[$a][$b][$c]...