How to echo data from a Multi-Dimensional Array in PHP?
You can't get all of Catalog
, Gauge
, Length
, and Tip Size
from the same $vardetails
element, they're in different elements of the array. You need to drill into each element to get its key and value.
You can create $vars_profile
in the loop that's processing the original array, you don't need $vargroup
.
To show the category only once, use a variable to hold the last value. Only output the category line when this field changes.
$vars_profile = '';
$last_metain = null;
foreach ($vararray as $vitem) {
foreach ($vitem as $metain => $vardetails) {
if ($metain != $last_metain) {
$vars_profile .= "<p>\nCatalog: $metain<br>\n";
$last_metain = $metain;
}
foreach ($vardetails as $key => $value) {
$vars_profile .= "$key: $value<br>\n";
}
}
}
return $vars_profile;