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

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

  • dongyong5255 2019-07-23 12:13
    关注

    I added logic here to filter out the project with the same URL as the page. Then select the random 3 projects to display it. Here is the demo 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
    }
    

    There were several issues in the code given in the question

    • spelling mistake in variable $recent_pProjects
    • no code for URL check
    • unused variables
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来