douwa1304
2019-07-23 10:23
浏览 264
已采纳

PHP foreach不循环遍历嵌套数组

I'll start by saying I'm new to PHP, but managed to hack together this foreach loop that runs through an array, but nothing is displaying and i'm getting an error saying 'invalid argument supplied for foreach()'.

The idea is that it should show 3 projects at random without repeating them and if the page URL matches the project_url variable, that particular project won't be shown.

Tried searching for similar issues but can't seem to find anything that fits my case as far as I can tell with my limited knowledge. Any help is greatly appreciated!

<?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条回答 默认 最新

相关推荐 更多相似问题