简体   繁体   English

Foreach未在多维数组中显示所有项目

[英]Foreach not showing all items in multidimensional array

This is my first question in a long time, any help is greatly appreciated! 这是我长期以来的第一个问题,非常感谢您的帮助!

I've got one database storing vehicles and one database storing their images. 我有一个存储车辆的数据库和一个存储其图像的数据库。 I am using an INNER JOIN to grab a list of vehicles and their images. 我正在使用INNER JOIN来获取车辆及其图像的列表。 After the database query, I put the return into an array; 数据库查询后,我将返回值放入数组; so 2 arrays in 1 array: 所以1个数组中有2个数组:

return array($vehicles, $vehicle_images);

When I do a print_r I get the correct return: 当我执行print_r时,我得到正确的回报:

<?php print_r($your_listings[0]); ?>
<br />
<?php print_r($your_listings[1]); ?>

Returns: 返回值:

Array
(
    [0] => Array
        (
            [vehicle_id] => 35
            [vehicle_type] => jeep
            [vehicle_vin] => 6969
            [owner_email] => user@user.com
            [vehicle_make] => Jeep
            [vehicle_year] => 2008
            [vehicle_model] => cherokee
        )

    [1] => Array
        (
            [vehicle_id] => 36
            [vehicle_type] => motorcycle
            [vehicle_vin] => 1234
            [owner_email] => user@user.com
            [vehicle_make] => honda
            [vehicle_year] => 2018
            [vehicle_model] => random
        )

    [2] => Array
        (
            [vehicle_id] => 39
            [vehicle_type] => atv
            [vehicle_vin] => 3215
            [owner_email] => user@user.com
            [vehicle_make] => Yamaha
            [vehicle_year] => 1990
            [vehicle_model] => OHYEA
        )

)

Array
(
    [0] => Array
        (
            [vehicle_id] => 35
            [image_display] => placeholder
        )

    [1] => Array
        (
            [vehicle_id] => 36
            [image_display] => /new/images/vehicles/users/42/image.jpg
        )

    [2] => Array
        (
            [vehicle_id] => 36
            [image_display] => /new/images/vehicles/users/42/vehicle1.jpg
        )

    [3] => Array
        (
            [vehicle_id] => 35
            [image_display] => /new/images/vehicles/users/42/vehicle.jpg
        )

    [4] => Array
        (
            [vehicle_id] => 39
            [image_display] => placeholder
        )

)

Now when I do a foreach (including bootstrap 4 styling), it only shows 2 vehicles instead of 3; 现在,当我进行一次foreach(包括bootstrap 4样式)时,它仅显示2辆车,而不是3辆; the 2 vehicles it shows appear to be showing exactly as I want them: 它显示的2辆车似乎完全按照我的要求显示:

<div class="container-fluid">
 <div class="row no-gutters">
<?php
$your_listings = owner_listings($_SESSION['user']);

if (!($your_listings[0])) {
  echo '<div class="col-sm"><div class="alert alert-danger" role="alert"><i class="fas fa-exclamation"></i> You do not have any listings active at this time.</div></div>';
}
else {
 foreach ($your_listings as $i => $item) {
  $make = $your_listings[0][$i]['vehicle_make'];
  $model = $your_listings[0][$i]['vehicle_model'];
  $year = $your_listings[0][$i]['vehicle_year'];
  $vehicle = $your_listings[0][$i]['vehicle_id'];
  $image = $your_listings[1][$i]['image_display'];
    if ($image != 'placeholder') {
      echo '<div class="col-sm"><div class="card" style="width: 18rem;">
            <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
            <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
            <div class="card-body">
              <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
            </div>
            </div></div>';
          }
          else {
            if ($your_listings[0][$i]['vehicle_type'] == 'atv') {
              $image = '/new/images/vehicles/types/atv.png';
              }
            elseif ($your_listings[0][$i]['vehicle_type'] == 'jeep') {
              $image = '/new/images/vehicles/types/jeep.png';
              }
            elseif ($your_listings[0][$i]['vehicle_type'] == 'motorcycle') {
              $image = '/new/images/vehicles/types/motorchycle.png';
              }
            echo '<div class="col-sm"><div class="card" style="width: 18rem;">
            <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
            <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
            <div class="card-body">
              <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
            </div>
            </div></div>';
          }
        }
  }
  ?>

</div>
</div>

Have I just been staring at this too long? 我只是盯着这个太久了吗? What am I doing wrong? 我究竟做错了什么? Any help is appreciated. 任何帮助表示赞赏。

Thanks! 谢谢!

You are looping original array which has two arrays as you said. 您正在循环具有您所说的两个数组的原始数组。 What you want is to loop through only first element of your_listings array to get three vehicles 您想要的是仅your_listings数组的第一个元素以获得三辆车

if (!($your_listings[0])) {
  echo '<div class="col-sm"><div class="alert alert-danger" role="alert"><i class="fas fa-exclamation"></i> You do not have any listings active at this time.</div></div>';
}
else {
foreach ($your_listings as $i => $item) {    // should be foreach ($your_listings[0] as $i => $item) {
$make = $item['vehicle_make'];
$model = $item['vehicle_model'];

Give a try to this answer... 试试这个答案...

<div class="container-fluid">
    <div class="row no-gutters">
    <?php
        $your_listings = owner_listings($_SESSION['user']);

        if (!($your_listings[0])) 
        {
          echo '<div class="col-sm"><div class="alert alert-danger" role="alert"><i class="fas fa-exclamation"></i> You do not have any listings active at this time.</div></div>';
        }
        else 
        {
            $newarray = array();
            foreach($your_listings[0] as $i => $item)
            {
                $newarray[$item["vehicle_id"]] = $item["image_display"];
            }
            foreach ($your_listings[0] as $i => $item) 
            {
              $make = $item['vehicle_make'];
              $model = $item['vehicle_model'];
              $year = $item['vehicle_year'];
              $vehicle = $item['vehicle_id'];         
              $image = $newarray[$vehicle];
                if ($image != 'placeholder') 
                {
                    echo '<div class="col-sm"><div class="card" style="width: 18rem;">
                        <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
                        <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
                        <div class="card-body">
                          <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
                        </div>
                        </div></div>';
                }
                else 
                {
                    if ($item['vehicle_type'] == 'atv') {
                      $image = '/new/images/vehicles/types/atv.png';
                      }
                    elseif ($item['vehicle_type'] == 'jeep') {
                      $image = '/new/images/vehicles/types/jeep.png';
                      }
                    elseif ($item['vehicle_type'] == 'motorcycle') {
                      $image = '/new/images/vehicles/types/motorchycle.png';
                      }
                    echo '<div class="col-sm"><div class="card" style="width: 18rem;">
                    <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
                    <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
                    <div class="card-body">
                      <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
                    </div>
                    </div></div>';
                }
            }
        }
    ?>
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM