Reputation: 55
How to nicely display the data structure. Below paste my multidimensional array, please help
Array
(
[1] => Array
(
[0] => Array
(
[LP] => 1
[Produkt] => product1
)
)
[2] => Array
(
[0] => Array
(
[LP] => 2
[number] => 157/03/2014
[Produkt] => product1
)
[1] => Array
(
[LP] => 2
[number] => 157/03/2014
[Produkt] => product2
=== My CODE ==============================================
foreach ($a as $date) {
$output .= "\t\t\t<ID>". $date[0]['LP']."</ID>\n";
second foreach for product {
$output .= "\t\t\t<Produkt>". Produkt ."</Produkt>\n";
}
}
Because I want to have something like this:
1
-Product1
2
-Product1
-Product2
3
-Product1
4
-Product1
-Product2
Upvotes: 0
Views: 90
Reputation: 78994
foreach($a as $array) {
foreach($array as $value) {
echo $value['LP'];
//etc...
}
}
Upvotes: 1
Reputation: 3813
You can nest foreach loops to get down to the deeper elements of your array.
foreach ($a as $array) {
foreach ($array as $key => $value) {
$output .= "\t\t\t<ID>". $value['LP']."</ID>\n";
$output .= "\t\t\t<Produkt>". $value['Produkt'] ."</Produkt>\n";
}
}
Upvotes: 1
Reputation: 147
Because it appears that you want your output to be XML, i'd suggest the following code:
function arraytoxml($xmlObj,$data) {
foreach ($data as $key => $value) {
if(is_array($value)) {
if(is_numeric($key)) {
$key = 'item_' . $key;
}
$subnode = $xmlObj->addChild("$key");
arraytoxml($subnode,$value);
} else {
$xmlObj->addChild("$key","$value");
}
}
return $xmlObj;
}
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><return></return>");
$return_xml = arraytoxml($xml,$data);
print($return_xml);
Where $data is your associative array.
Upvotes: 2