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条)

报告相同问题?

悬赏问题

  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 各位佬,下面的问题怎么用python实现?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化