I follow struggling with bidimensional arrays and foreach loops. The following code shows a magazines array, each of them points to an unidimensional Articles array. Right now is storing three Articles for each magazine (just the Article’s Title). I would like to know how to associate a Bidimensional array so that can I store the Author and PagesNumber besides the Title, for each article belonging a magazine. Could you point me in the right direction? Thanks in advance.
<?php
// Create a magazines array
$magazines = array();
// Put 5 magazines on it
for($x=0;$x<5;$x++)
$magazines[] = "Magazine " . $x ;
// Associate articles array to each magazine
foreach($magazines as $magazine){
$articles[$magazine] = array();
for($x=0;$x<3;$x++)
$articles[$magazine][] = $magazine . " - Article Title " . $x ;
}
// List them all
foreach($magazines as $magazine){
echo $magazine . " has these articles: <br>";
foreach($articles[$magazine] as $article)
echo $article . "</br>";
}
?>
Maybe you should structure it a little different:
A little explaination:
Magazines is a normal array.
Then you add associative arrays to it (magazines).
Each magzines-array has an articles-field, which is a normal array, which you add associative arrays to again (articles).
So now, you iterate throug magazines, and each magazine has an array of articles which you can iterate.
That means: Magazines is an array of single magazines, one magazine is an associative array, which also contains an array of articles. An article is an associative array again.
I hope that sounds not too confusing… 😉