Foreach循环中的动态数组名称


Dynamic Array Name in Foreach Loop?

我有一个页面,上面有几个链接,比如grade.php?item=幼儿园,其中项目值不同。我的目标是每节课都有详细的页面。

在目标页面,一个详细信息页面上,我使用foreach循环,但我无法使其动态工作。

<?php foreach ($classProjects as $grade => $item) { ?> 
  <li><?php echo $item[image]; ?><a href="grade.php?item=<?php echo $grade; ?>"><?php echo $item[title]; ?></a><br /><p><?php echo $item[sentence]; ?></p><br /><a href="grade.php?item=<?php echo $grade; ?>">More &rsaquo;&rsaquo;</a></li>
< } ?>

只要使用了$classProjects,详细信息就会正确列出,但根据项目名称的不同,会有不同的详细信息。我为每个类都有单独的数组。我试图使数组名称动态化,但没有成功。。。。即使我能够回显正确的字符串。。。。并将其类型更改为数组。

也许我处理这个问题的方式不对?我最好修改我的数组以包括另一层深度,而不是试图捕获项值并在foreach循环中动态命名我的数组吗?

我可以举个例子。谢谢

@DarylGill附加信息:

  1. 您正在使用的数组的内容(这是以相同方式设置的6个数组之一)

$kinderProjects=数组(

                    array(
                            title       => "Fall Leaves",
                            blurb       => "Our Kinders have enjoyed learning the difference between the warm colors (yellow, orange, red) and the cool colors (green, blue, violet) and are putting this knowledge to use by painting fall leaves in watercolor and oil pastel. They are also reviewing the use of the visual art elements of line and shape.",
                            image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
                         ),
                    array(
                            title       => "Francis",
                            blurb       => "Francis knows her stuff. The big sister of Frankie himself, she runs the show. Don't miss her Margherita Mondays!",
                            image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
                         ),
                    array(
                            title       => "Carlos",
                            blurb       => "Carlos is the epitome of the phrase &ldquo;Don't judge a book by it's cover&rdquo; &mdash; You simply cannot find a better chef.",
                            image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
                         ),
               );
  1. 您的预期结果

选择上一页上的每个链接将引导用户进入该特定等级的详细页面(即,从该等级的正确数组中提取)。

  1. 预期结果需要什么信息

详细信息页面上的foreach循环不能有一个指向特定数组的硬连线指针,否则所有6个链接都将指向该数据。它需要捕获在URL中传递的项值。我想这样做的一种方法是制作另一个变量,该变量将动态更新foreach循环中的数组名称。

<?php foreach ($classProjects as $grade => $item) { ?> 

如果$classProjects可以在单击幼儿园链接时动态更新为"$kinderProjects",那么它将从该数组中提取正确的数据。

$count = count($classProjects);
for($i = 0 ;$i < $count ; $i++){
 echo $classProjects[$i];
 echo $anotherArray[$i];//or what ever you want to print here.
}

您可以根据参数请求数据,然后将其分配给正在循环的数组。

例如:

$data_array_one = array(
    array(
        title       => "Fall Leaves",
        blurb       => "Our Kinders have enjoyed learning the difference between the warm colors (yellow, orange, red) and the cool colors (green, blue, violet) and are putting this knowledge to use by painting fall leaves in watercolor and oil pastel. They are also reviewing the use of the visual art elements of line and shape.",
        image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
    ),
    array(
        title       => "Francis",
        blurb       => "Francis knows her stuff. The big sister of Frankie himself, she runs the show. Don't miss her Margherita Mondays!",
        image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
    )
);
$data_array_two = array(
    array(
        title       => "Fall Leaves",
        blurb       => "Our Kinders have enjoyed learning the difference between the warm colors (yellow, orange, red) and the cool colors (green, blue, violet) and are putting this knowledge to use by painting fall leaves in watercolor and oil pastel. They are also reviewing the use of the visual art elements of line and shape.",
        image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
    ),
    array(
        title       => "Francis",
        blurb       => "Francis knows her stuff. The big sister of Frankie himself, she runs the show. Don't miss her Margherita Mondays!",
        image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
    )
);
switch ($_GET['item']) {
    case 'kindergarten':
        $classProject = $data_array_one;
        break;
    case 'tiergarten':
        $classProject = $data_array_two;
        break;
    default:
        // Define the default data here
        $classProject = array();
        break;
}
foreach ($classProjects as $grade => $item) {
    echo '<li>'. $item['image'] .'<a href="grade.php?item='. $grade .'">'. $item['title'] .'</a><br /><p>'. $item['sentence'] .'</p><br /><a href="grade.php?item='. $grade .'">More &rsaquo;&rsaquo;</a></li>';
}