Question

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>
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top