PHP Array to List
//code by acmol
function array2ul($array) {
$out = "<ul>";
foreach($array as $key => $elem){
if(!is_array($elem)){
$out .= "<li><span>$key:[$elem]</span></li>";
}
else $out .= "<li><span>$key</span>".array2ul($elem)."</li>";
}
$out .= "</ul>";
return $out;
}
I think you are looking for this.
Here's a much more maintainable way to do it than to echo html...
<ul>
<?php foreach( $array as $city => $hotels ): ?>
<li><?= $city ?>
<ul>
<?php foreach( $hotels as $hotel ): ?>
<li><?= $hotel ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
Here's another way using h2s for the cities and not nested lists
<?php foreach( $array as $city => $hotels ): ?>
<h2><?= $city ?></h2>
<ul>
<?php foreach( $hotels as $hotel ): ?>
<li><?= $hotel ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
The outputted html isn't in the prettiest format but you can fix that. It's all about whether you want pretty html or easier to read code. I'm all for easier to read code =)