我跟随二维数组和foreach循环的努力。下面的代码显示了一个杂志数组,每个数组都指向一维的文章数组。现在正在为每个杂志存储三篇文章(只是文章的标题)。我想知道如何关联一个Bidimensional数组,以便我可以存储除Title之外的Author和PagesNumber,对于属于杂志的每篇文章。你能指出我正确的方向吗?提前谢谢。
<?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>";
}
?>
答案 0 :(得分:1)
您需要将额外信息存储在杂志条目中的关联数组中:
for ( $m = 0; $m < 5; $m++ ) {
for ( $a = 0; $a < 3; $a++ ) {
$magazines["Magazine $m"]["Article $a"] = array(
'title' => "Title $a",
'author' => "Author $a"
);
}
}
然后,您可以通过多种方式访问数组中的每个元素:
// foreach loops through each element of the array,
// returning the key and value:
foreach ( $magazines as $magazine => $articles ) {
print "$magazine\n";
foreach ( $articles as $article => $info ) {
print " $article\n";
// You can also access elements directly by their key:
print " Title: {$info['title']}\n";
print " Author: {$info['author']}\n";
}
}
结果:
Magazine 0
Article 0
Title: Title 0
Author: Author 0
Article 1
Title: Title 1
Author: Author 1
Article 2
Title: Title 2
Author: Author 2
Magazine 1
Article 0
Title: Title 0
Author: Author 0
...
创建嵌套数组的方法有很多种,选择取决于您的要求。上面的方法只使用了关联数组,而下面使用了关联数组和数值数组:
$magazines = array();
for ( $m = 0; $m < 5; $m++ ) {
$magazines[$m] = array( 'title' => "Magazine $m" );
for ( $a = 0; $a < 3; $a++ ) {
$magazines[$m]['articles'][] = array(
'title' => "Article $a",
'author' => "Author $a"
);
}
}
答案 1 :(得分:1)
// 5 articles per magazine:
$articles = array_combine($magazines, array_fill(0, 5, array(1,2,3,4,5));
现在每个杂志都有5篇文章,其中有一个数字作为标题。只是为了演示。希望能帮助到你。您可以通过$articles
数组访问所有文章,然后将杂志标题作为密钥。
当然,您也可以手动设置每个杂志的文章:
$articles[$magazine] = array('My first article', 'Editorial', 'Cooking with wood fire');
甚至个别:
$articles[$magazine][] = 'Learning by doing.';
修改:如果某篇文章提供的内容更多,则标题为:
$articles[$magazine][] = array
(
'title' => 'Learning by doing.',
'author' => 'Frank Stein',
'pages' => 2,
);
答案 2 :(得分:1)
我不确定我是否在这里关注这个问题。为什么不使用以下内容?
$magazines = array(
array( // a single magazine
'title' => 'Issue 1',
'articles' => array( // a list of articles
array( // a single article
title => 'Article 1',
content => 'xyz'
)
)
),
);
编辑:修正了小错误:)
如果您想以更易读的方式制作它,您可以这样做:
function Magazines(){
return func_get_args();
}
function Magazine($title,$articles){
return array(
'title' => $title,
'articles' => $articles
);
}
function Articles(){
return func_get_args();
}
function Article($title,$content){
return array(
'title' => $title,
'content' => $content
);
}
// and using it...
$magazines = Magazines(Magazine('Issue 1',Articles(Article('Article 1','xyz'))));
它只是用一些好的命名取代array()
。您可以轻松地将此(或类似的东西)转换为OOP。最后,OOP仅用于管理项目。实际存储始终是一个数组。
<强>穿越强>
一旦你有了正确的结构,通过数组读取实际上非常简单:
foreach($magazines as $magazine){
// user things like $magazine['title'] in here...
foreach($magazine['articles'] as $article){
// user $article['title'] here
}
}
这是 6 行的干净代码。
答案 3 :(得分:1)
也许你应该把它的结构略有不同:
$magazines = array();
//create magazines
for($x=0;$x<5;$x++) {
$magazines[] = array('name' => 'Magazine ' . $x,
'year' => '2011',
'articles' => array());
}
//fill in some articles
foreach ($magazines as &$magazine) {
for($x=0;$x<3;$x++) {
$magazine['articles'][] = array('article_titel' => $magazine['name'] . " - Article Title " . $x,
'article_author' => 'author ' . $x,
'article_page' => $x);
}
}
//output
foreach($magazines as $magazine) {
echo 'The Magazine ' . $magazine['name'] . ' from ' . $magazine['year'] . ' contains the articles : ';
foreach($magazine['articles'] as $article) {
echo $article['article_titel'] . ' by ' . $article['article_author'] . ' on page ' . $article['article_page'];
}
}
一点解释: 杂志是一个正常的阵列。 然后你添加关联数组(杂志)。 每个magzines-array都有一个articles-field,这是一个普通的数组,你可以再次添加关联数组(文章)。
所以现在,你通过杂志进行迭代,每个杂志都有一系列你可以迭代的文章。
这意味着:杂志是一系列单个杂志,一个杂志是一个关联数组,其中还包含一系列文章。一篇文章再次是一个关联数组。
我希望听起来不会太混乱......;)
答案 4 :(得分:0)
怎么样:
$magazines[$magazine] = array();
$magazines[$magazine]['article']['title'] = 'title';
$magazines[$magazine]['article']['other'] = 'other';
foreach ($magazines as $name => $magazine) {
echo "name: " . $name;
foreach ($magazine as $articleName => $article) {
echo "title: " . $article['title'] . "\n";
echo "other: " . $article['other'] . "\n";
}
}