1

My array $array contains the following:

Array ( [0] => Array (
 [label] => Location and Contact
 [description] =>
 ) [1] => Array (
 [label] => Province
 [name] => province
 [options] => Array ( [0] => Province 1
 [1] => Province 2
 [2] => Province 3 )
 ) [2] => Array (
 [label] => City
 [name] => city
 [options] => Array ( [0] => City 1
 [1] => City 2
 [2] => City 3 )
 ) 

What I want to achieve is to loop those three cities at the bottom, probably with the use of the [name] => city.

What I've tried so far (which isn't really looking good):

foreach ($array as $arr) {
 foreach ($arr['options'] as $option) {
 ?>
 &raquo; <?php echo $option; ?><br />
 <?php
 }
}

My obvious problem with the code is the foreach loop within foreach loop plus I haven't figured out how to identify [name] => city from [name] => province, both of them having [options].

I'm fairly new to looping arrays.

UPDATE (WITH MY ANSWER)

Combining worldofjr's answer and my modification so I can enclose each loop in a container like <li>, <option>, <div>, etc, I just created two foreach's:

 foreach($array as $arr) {
 if($arr['name'] == "city") {
 $cities = $arr['options'];
 }
 }
 echo '<select>';
 foreach($cities as $city){
 echo '<option value="'.$city.'" class="class1 class2" data-att="att">'.$city.'</option>';
 }
 echo '</select>';
asked Sep 16, 2014 at 17:22
7
  • If you want to display only cities or provinces then use array_filter Commented Sep 16, 2014 at 17:25
  • You have not made it very clean what you want the end result to be. If you clarify your question you will get some better answers. Commented Sep 16, 2014 at 17:47
  • @mschuett To requote the end result I wanted: "What I want to achieve is to loop those three cities at the bottom, probably with the use of the [name] => city." Commented Sep 17, 2014 at 1:11
  • To display each city in a list, you don't need the 2nd foreach. Just wrap the implode() with list tags, ie; echo "<ul><li>" . implode("</li><li>", $arr['options']) . "</li></ul>"; Commented Sep 17, 2014 at 14:10
  • 1
    Fair enough. It does depend on your particular situation, but if you do have a problem (and can't find an answer on SO) do ask another question. Commented Sep 17, 2014 at 22:51

2 Answers 2

2

You could use implode($glue,$array) as follows;

foreach($array as $arr) {
 if($arr['name'] == "city") {
 echo implode('<br>',$arr['options']);
 }
}

or if you want to print a list of the cities, wrap the implode() with list tags, like this;

foreach($array as $arr) {
 if($arr['name'] == "city") {
 echo "<ul><li>" . implode("</li><li>", $arr['options']) . "</li></ul>";
 }
}

See PHP manual: implode.

Hope this helps!

answered Sep 16, 2014 at 17:43
1
  • Helped a lot. Need to study more about implodes and explodes. Thank you. :) Commented Sep 17, 2014 at 6:28
1

I don't understand at all, but I what you want is simply output the three cities, yo can do this

foreach ($array as $arr) 
{
 if ($arry['name'] == 'city') 
 {
 foreach ($arr['options'] as $option) 
 {
 echo $option . '<br />';
 }
 }
} 

That's enough?

answered Sep 16, 2014 at 17:30
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.