I am posting this message because I couldn't find the answer to my problem anywhere. I might not be looking for the right search query i am just starting with arrays.
I have the following array with nested arrays organized by information type (Name, URL, Date, Online) :
Array (
[Name] => Array ( [0] => Name 1 [1] => Name 2 )
[URL] => Array ( [0] => http://url-1.com [1] => http://url-2.com )
[Date] => Array ( [0] => 2014-05-31 11:10 [1] => 2014-05-26 11:16 )
[Online] => Array ( [0] => 1 [1] => ) )
The key is showing which items are linked. Every [0] belongs together for example :
- [URL][0] is the url of [Name][0]
I want to do a foreach displaying the value by key in order to echo something like this :
<a href="[URL][0]">[Name][0]</a><p>[Date][0]</p><p>[Online][0]</p>
<a href="[URL][1]">[Name][1]</a><p>[Date][1]</p><p>[Online][1]</p>
There can be 1 to n keys.
Found a Solution
I manage to get the array differently :
$array = Array (
[0] => Array ( [name] => Name 1 [url] => http://url-1.com [date] => 2014-05-31 11:10 [online] => 1 )
[1] => Array ( [name] => Name 2 [url] => http://url-2.com [date] => 2014-05-26 11:16 [online] => ) )
It's organized by arrays containing the values together, not by arrays containing all the names together, all the urls together,... If someone knows how to change the initial to this one I can add it to my solution.
Once I got this new array, it's way easier to manage. I got the loop to work with a for :
I first set a limit to the for by getting the highest array key value :
$max_stop = max(array_keys($array));
Then i did a for. The +1 after the $max_stop is needed, otherwise it stops counting at the second to last one (the count starts at 0 and not 1)
for ($row = 0; $row < $max_stop+1; $row++){
Then I echo what i wanted to display :
echo '<a href="'.$array[$row]["url"].'">'.$array[$row]["name"].'</a><p>'.$array[$row]["date"].'</p><p>'.$array[$row]["online"].'</p>';}
This may not be the best way but it works as I wanted.