PHP foreach不循环遍历嵌套数组

时间:2019-07-23 10:23:44

标签: php html arrays foreach

我首先要说我是PHP的新手,但设法将通过数组运行的这个foreach循环合并在一起,但是什么也没有显示,并且我收到一条错误消息,说“为foreach()提供了无效的参数'。

这个想法是,它应该随机显示3个项目而不重复它们,并且如果页面URL与project_url变量匹配,则该特定项目将不会显示。

尝试搜索类似的问题,但就我所知有限,似乎无法找到任何适合我的情况。任何帮助将不胜感激!

<?php

$recent_projects = array(
        array(
            project_img         => 'assets/img.jpg',
            project_title       => 'Project Name',
            project_cat         => 'Project Category',
            project_url         => 'project-name',
            project_alttag      => 'Project Name',
            project_titletag    => 'Project name',
            project_nofollow    => 'follow'
        ),
        array(
            project_img         => 'assets/img.jpg',
            project_title       => 'Project Name',
            project_cat         => 'Project Category',
            project_url         => 'project-name',
            project_alttag      => 'Project Name',
            project_titletag    => 'Project name',
            project_nofollow    => 'follow'
        ),
        array(
            project_img         => 'assets/img.jpg',
            project_title       => 'Project Name',
            project_cat         => 'Project Category',
            project_url         => 'project-name',
            project_alttag      => 'Project Name',
            project_titletag    => 'Project name',
            project_nofollow    => 'follow'
        ),
        array(
            project_img         => 'assets/img.jpg',
            project_title       => 'Project Name',
            project_cat         => 'Project Category',
            project_url         => 'project-name',
            project_alttag      => 'Project Name',
            project_titletag    => 'Project name',
            project_nofollow    => 'follow'
        )
    );

    $show_x_projects  = 3; // How may projects to show
    $i                = 0;
    $current_page     = trim($_SERVER['REQUEST_URI'], '/');
    $random_projects  = array_rand($recent_projects);

?>
      <?php foreach($random_projects as $recent_projects):
        $item = (object)$recent_projects;
        if($current_page == $item->project_url) continue;
        $i++;
        if($i >= $show_x_projects) continue; ?>


          <div class="col-sm-12 col-md-4">
            <figure>
              <img class="img-fluid" src="<?php echo $item->project_img; ?>" alt="<?php echo $item->project_alttag; ?>" title="<?php echo $item->project_titletag; ?>">
              <figcaption>
                  <h4><span><?php echo $item->project_title; ?></span></h4>
                  <p><?php echo $item->project_cat; ?></p>
                  <a href="<?php echo $item->project_url; ?>" rel="<?php echo $item->project_nofollow; ?>"></a>
                  <p class="view-project-link">View Project →</p>
              </figcaption>
            </figure>
          </div>

      <?php endforeach; ?>

3 个答案:

答案 0 :(得分:0)

首先...您的数组应如下所示:

<?php

    $recent_projects = [
            [
                'project_img'         => 'assets/img.jpg',
                'project_title'       => 'Project Name',
                'project_cat'         => 'Project Category',
                'project_url'         => 'project-name',
                'project_alttag'      => 'Project Name',
                'project_titletag'    => 'Project name',
                'project_nofollow'    => 'follow'
            ],
            [
                'project_img'         => 'assets/img.jpg',
                'project_title'       => 'Project Name',
                'project_cat'         => 'Project Category',
                'project_url'         => 'project-name',
                'project_alttag'      => 'Project Name',
                'project_titletag'    => 'Project name',
                'project_nofollow'    => 'follow'
            ],
            [
                'project_img'         => 'assets/img.jpg',
                'project_title'       => 'Project Name',
                'project_cat'         => 'Project Category',
                'project_url'         => 'project-name',
                'project_alttag'      => 'Project Name',
                'project_titletag'    => 'Project name',
                'project_nofollow'    => 'follow'
            ],
            [
                'project_img'         => 'assets/img.jpg',
                'project_title'       => 'Project Name',
                'project_cat'         => 'Project Category',
                'project_url'         => 'project-name',
                'project_alttag'      => 'Project Name',
                'project_titletag'    => 'Project name',
                'project_nofollow'    => 'follow'
            ]
        ];

        $show_x_projects  = 3; // How may projects to show
        $i                = 0;
        $current_page     = trim($_SERVER['REQUEST_URI'], '/');
        $random_keys= array_rand($recent_projects,3);

    ?>

比起循环部分:

<?php
    foreach($random_keys as $key): ?>
            <?php $project = $recent_projects[$key];?>
          <div class="col-sm-12 col-md-4">
            <figure>
              <img class="img-fluid" src="<?php echo $project["project_img"]; ?>" alt="<?php echo $project["project_alttag"]; ?>" title="<?php echo $project["project_titletag"]; ?>">
              <p></p>
              <figcaption>
                  <h4><span><?php echo $project["project_title"]; ?></span></h4>
                  <p><?php echo $project["project_cat"]; ?></p>
                  <a href="<?php echo $project["project_url"]; ?>" rel="<?php echo $project["project_nofollow"]; ?>"></a>
                  <p class="view-project-link">View Project →</p>
              </figcaption>
            </figure>
          </div>

      <?php endforeach; ?>

Working php fiddle

答案 1 :(得分:0)

请用以下代码替换您的代码:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <?php
        $recent_projects = array(
            array(
                'project_img' => 'assets/img.jpg',
                'project_title' => 'Project Name',
                'project_cat' => 'Project Category',
                'project_url' => 'project-name',
                'project_alttag' => 'Project Name',
                'project_titletag' => 'Project name',
                'project_nofollow' => 'follow'
            ),
            array(
                'project_img' => 'assets/img.jpg',
                'project_title' => 'Project Name',
                'project_cat' => 'Project Category',
                'project_url' => 'project-name',
                'project_alttag' => 'Project Name',
                'project_titletag' => 'Project name',
                'project_nofollow' => 'follow'
            ),
            array(
                'project_img' => 'assets/img.jpg',
                'project_title' => 'Project Name',
                'project_cat' => 'Project Category',
                'project_url' => 'project-name',
                'project_alttag' => 'Project Name',
                'project_titletag' => 'Project name',
                'project_nofollow' => 'follow'
            ),
            array(
                'project_img' => 'assets/img.jpg',
                'project_title' => 'Project Name',
                'project_cat' => 'Project Category',
                'project_url' => 'project-name',
                'project_alttag' => 'Project Name',
                'project_titletag' => 'Project name',
                'project_nofollow' => 'follow'
            )
        );

        $show_x_projects = 3; // How may projects to show
        $i = 0;
        $current_page = trim($_SERVER['REQUEST_URI'], '/');
        $random_projects = array_rand($recent_projects);
        ?>
        <?php $random_projects = array_rand($recent_projects, 3);
        foreach ($random_projects as $index): 
            $item = $recent_projects[$index];
            ?>

            <div class="col-sm-12 col-md-4">
                <figure>
                    <img class="img-fluid" src="<?php echo $item['project_img']; ?>" alt="<?php echo $item['project_alttag']; ?>" title="<?php echo $item['project_titletag']; ?>">
                    <p></p>
                    <figcaption>
                        <h4><span><?php echo $item['project_title']; ?></span></h4>
                        <p><?php echo $item['project_cat']; ?></p>
                        <a href="<?php echo $item['project_url']; ?>" rel="<?php echo $item['project_nofollow']; ?>"></a>
                        <p class="view-project-link">View Project →</p>
                    </figcaption>
                </figure>
            </div>

<?php endforeach; ?>
    </body>
</html>

答案 2 :(得分:0)

我在这里添加了逻辑,以使用与页面相同的URL过滤出项目。然后选择随机的3个项目进行显示。这是演示link

$recent_projects = [
    [
        'project_title'       => 'Project Name1',
        'project_url'         => 'url1'
    ],
    [
        'project_title'       => 'Project Name2',
        'project_url'         => 'url2'
    ],
    [
        'project_title'       => 'Project Name3',
        'project_url'         => 'url3'
    ],
    [
        'project_title'       => 'Project Name4',
        'project_url'         => 'url4'
    ],
    [
        'project_title'       => 'Project Name5',
        'project_url'         => 'url5'
    ]
];
$show_x_projects  = 3; // How may projects to show
$current_page     = trim($_SERVER['REQUEST_URI'], '/');

// remove the project which matches with page url
$filtered_projects = array_filter($recent_projects, function ($project) use ($current_page) {
    return ($project['project_url'] != $current_page);
});

// select random 3 project from filtered projects
$random_projects  = array_rand($filtered_projects, $show_x_projects);

foreach ($random_projects as $index) {
    $item = $filtered_projects[$index];
    echo $item['project_title']; // replace this with hrml code
}

问题代码给出了几个问题

  • 变量$recent_pProjects中的拼写错误
  • 没有用于URL检查的代码
  • 未使用的变量