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

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
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxiaoyan4388 2019-07-23 10:48

    First...Your array should look like this:

    <?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);
    
        ?>
    

    Than for the looping part:

    <?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

    评论
    解决 无用
    打赏 举报
  • duansen6750 2019-07-23 10:58

    Please replace your code with this code:

    <!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>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题