0

I am trying to convert an array (of arrays?) to a simple unordered list. I came up with one solution, but it was very bulky, and I am positive there is a much simpler way to to do it.

This is the array

Array ( 
    [0] => Array ( [projects_id] => 204 [City] => Carrollton ) 
    [1] => Array ( [projects_id] => 2 [City] => Dallas ) 
    [2] => Array ( [projects_id] => 208 [City] => Garland ) 
    [3] => Array ( [projects_id] => 1 [City] => Plano ) 
    [4] => Array ( [projects_id] => 212 [City] => Richardson ) 
)

and the list should look something like this

<ul>
    <li><a href="#">Carrollton</a>
    <li><a href="#">Dallas</a>
    <li><a href="#">Garland</a>
    <li><a href="#">Plano</a>    
    <li><a href="#">Richardson</a>
</ul>
3
  • 2
    So… which way did you try that was bulky? Commented Sep 29, 2013 at 0:50
  • I was using a recursion that actually got stuck in an endless loop... Commented Sep 29, 2013 at 0:52
  • 1
    Why recursion for a simple thing like this? You iterate the array. Commented Sep 29, 2013 at 1:16

1 Answer 1

3

This seems like just about the only way to do it, and it’s pretty straightforward…

<ul>
    <?php foreach($arr as $item): ?>
        <li><a href="#"><?php echo htmlspecialchars($item['City']); ?></a></li>
    <?php endforeach; ?>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

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.