Forum Moderators: coopster

Message Too Old, No Replies

loop through array

multidimensional array looping

         

Dunjohn19

12:28 pm on Aug 8, 2019 (gmt 0)

5+ Year Member



Dear members,

I am faced with a problem and i am not sure how to proceed. I am hoping that someone can offer small examples of code. I think that this should be a simple block of code but i may not be doing it correctly. I am a beginner so i do not know if my idea is the best method or not. Please advise me of how to proceed and any examples will be most appreciated.

data needing a loop so that i don't have to hard code the pages.
i am organizing data by state and city. Some states have several cities listed and some only one city.
so: title of div block is state. divs contained in the state block are cities which have images of the cities.

div
title Florida
div city Miami
div city Orlando
/div

div
title California
div city Los Angeles
div city San Diego
div city San Francisco
/div

i was thinking that i could build an array of states and in each state another array with cities. then loop through each state then through each city. Basically for each state write the divs that contain them plus the state name as title. then for each city in each state, write the divs that contain the cities plus their names to be displayed.

I researched looping through multidimensional arrays but it is complicated and i am not sure if this is the best and only method to accomplish my tasks/solve my problem. I need each state to contain each city and i need their names to be displayed (echo state echo city*amount of cities under the state. I also use the city names to link an image (Orlando.jpg).

how i can do this? like so?


$state = array(
array('California'
array('Los Angeles', 'San Diego', 'San Francisco')
)
array('Florida',
array('Orlando', 'Miami')
)
);


then i should loop through each array? but i am not sure how to do this but also is this the best method?

Thank you very much.

Dimitri

1:38 pm on Aug 8, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



foreach($state as $state_info)
{
echo '<div>';
echo $state_info[0];
foreach($state_info[1]??[] as $city)
{
 echo '<div>',$city,'</div>';
}
echo '</div>';
}

Dimitri

2:48 pm on Aug 8, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



also is this the best method?

It depends what you want to do exactly, or which kind of other operations you want to do with this array.

You might also consider:
$state=[
'California'=>['Los Angeles','San Diego','San Francisco'],
'Florida'=>['Orlando','Miami']
];


In that case, the above code becomes:

foreach($state as $name=>$cities)
{
echo '<div>';
echo $name;
foreach($cities as $city)
{
 echo '<div>',$city,'</div>';
}
echo '</div>';
}


It really depends of what you want to achieve and how.

Dunjohn19

3:17 pm on Aug 8, 2019 (gmt 0)

5+ Year Member



Hello Dimitri,

i managed to get it working on my own because i didn't expect such a quick response. I usually have to wait a day or more in most forums. I came back to post that i got it working and i was surprised to see your reply! Thank you, Sir.

the code that i came up with is similar to what you posted except i used the word array. like so:


$state=array(
'California'=>array('Los Angeles','San Diego','San Francisco'),
'Florida'=>array('Orlando','Miami')
);
foreach($state as $name=>$cities)
{
echo '<div>';
echo $name;
foreach($cities as $city)
{
echo '<div>',$city,'</div>';
}
echo '</div>';
}


so i didn't know that i could simply type this:

$state=[
'California'=>['Los Angeles','San Diego','San Francisco'],
'Florida'=>['Orlando','Miami']
];


is it better to use brackets instead of array()?

Dmitri, you are wonderful! i am surprised to see such a quality answer in an online forum. i usually get replies that want your lifestory and want to know what you are building instead of just asnswering the question. You get right to the answer and add a subtle 'It really depends of what you want to achieve and how.'

Folks, Dmitri is Great! I like this reponse and this forum!

I don't see a solution button or any accolades button. However, this question is resolved by Dmitri.

Dimitri

3:24 pm on Aug 8, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



is it better to use brackets instead of array()?

No, it's exactly the same. Use what you feel the more comfortable with. I prefer brackets because "I" think it's easier to read. But it's just personal feeling.

Folks, Dmitri is Great! I like this reponse and this forum!

Shhh, otherwise it will be found out that this is a set up to increase my "votes" counter...

Dunjohn19

3:52 pm on Aug 8, 2019 (gmt 0)

5+ Year Member



Hello Dmitri,

Thank you for clarifying the use of brackets. I actually like the brackets so i will just follow your lead. less typing and same result :-D

I don't really know how the voting works but you should be maxed out! like higher than Senior Member. You've made my first post a pleasant one. I will definitely recommend this forum to other people in need of php guidance. Really, most forums are like a bumpy dirt road with pot holes and obstacles. You make this place feel like a reshly paved highway with wide lanes. What a relief to actually have someone reply with an attempt to solve a problem. I tried a forum years ago and the moderator replied to my question with more questions and more questions instead of answering the problem. I felt like it was opposite of a php programming forum.

You have made this a nice experience for me. I like this forum. If i can help others here, then i will do so whenever possible. I am happy to be a member. To forum Admins: Dmitri should be maxed out in votes and promoted!

Thank you, Dmitri and may you have a splendid day :-D