9

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...

1
  • so many things. This is my last gasp Commented Nov 28, 2009 at 17:16

4 Answers 4

24
//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.

Sign up to request clarification or add additional context in comments.

2 Comments

Rockstar code right here. Thanks for saving me the time of building it!
I love that it's recursive
14

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

2 Comments

this is the correct answer. Perfect. Thanks for introducing me to endforeach. this is EXACTLY what I have been looking for.
@Ash you may note, that endforeach is used here for the sense of readability, so the ordinary foreach can be used. See php.net/manual/en/control-structures.alternative-syntax.php
9

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>';
}

Comments

0

Assume your data is in $array.

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

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

3 Comments

This isn't quite what was requested, but it is probably what was intended. Some CSS will complete the formatting.
actually the above code only provides one "child" for each parent, so for example Camden Town is only given one hotel listed instead of 2.
its recommended to use echo over print in PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.