English 中文(简体)
PHP Array to List
原标题:

How do I go from this multidimensional array:

Array (
  [Camden Town] => Array (
    [0] => La Dominican
    [1] => A Lounge
  ), 
  [Coastal] => Array (
    [0] => Royal Hotel
  ), 
  [Como] => Array (
    [0] => Casa Producto 
    [1] => Casa Wow
  ), 
  [Florence] => Array (
    [0] => Florenciana Hotel
  )
)

to this:

<ul>
  <li>Camden Town</li>
  <ul>
    <li>La Dominican</li>
    <li>A Lounge</li>
  </ul>
  <li>Coastal</li>
  <ul>
    <li>Royal Hotel</li>
  </ul>
  ...
</ul>

above is in html...

最佳回答

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 =)

问题回答
//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.

Refactored acmol s funciton

/**
 * Converts a multi-level array to UL list.
 */
function array2ul($array) {
  $output =  <ul> ;
  foreach ($array as $key => $value) {
    $function = is_array($value) ? __FUNCTION__ :  htmlspecialchars ;
    $output .=  <li><b>  . $key .  :</b> <i>  . $function($value) .  </i></li> ;
  }
  return $output .  </ul> ;
}

Assume your data is in $array.

echo  <ul> ;
foreach ($array as $city => $hotels)
{
    echo "<li>$city</li>
<ul>
";
    foreach ($hotels as $hotel)
    {
        echo "    <li>$hotel</li>
";
    }
    echo "</ul>

";
}
echo  </ul> ;

Haven t tested it, but I m pretty sure it s right.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签