首页 > 解决方案 > 尝试访问 int 类型值的数组偏移量 - 对于每个循环

问题描述

所以在我的代码中,我有 2 个函数。一种将返回表中的所有行,另一种将根据其返回 1 行code

当我运行该函数以获取所有这些函数时 - 没有问题 - 使用foreach循环可以正常工作并显示在 html 上。

但是,当我运行该函数以获取一个函数时,我在引用数组的每一行的标题中出现错误,例如$location['address_line2']

我知道这是较新版本的 php 的错误,但找不到解决方法。

顺便说一句,查询工作正常,我用来print_r在 foreach 循环之前显示它,我可以在检查元素中看到它。

$premises = getPremisesByCode($connection, $premisesCode);
//$premises = getAllPremises($connection);

if($premises != NULL){
  foreach ($premises as $location) {
    $visible = ($location['active'] != 'deactivated' ? '<span class="fa-stack"><i style="color: #008d1b;" class="fa fa-check fa-stack-1x"></i></span>' : '<span class="fa-stack"><i style="color: #8d0000;" class="fa fa-times fa-stack-1x"></i></span>' );

    $editBtn = '<a class="btn btn-xs btn-info px-2 py-1 shadow" href="/'.$productfolder.'/edit-product-type/'.$location['code'].'/" title="Edit this product"><i style="font-size: 1rem;" class="mdi mdi-lead-pencil"></i></a>&nbsp;';

    $toggleIcon = ($location['active'] != 'deactivated') ? '<i class="mdi mdi-close"></i>' : '<i class="mdi mdi-check"></i>';
    $toggleColour = ($location['active'] != 'deactivated') ? 'warning' : 'success';
    $toggleBtn = '<a class="btn btn-xs btn-'.$toggleColour.' px-2 py-1 shadow" href="/'.$productfolder.'/toggle-premises/'.$location['code'].'/">'.$toggleIcon.'</a>&nbsp;';

    $ptImage = (isset($location['logo'])) ? '<img class="img-thumbnail shadow mx-auto d-block p-2" style="max-width: 200px; background-color: '.$location['primary_colour'].' " src="/assets/images/'.$location['logo'].'">' : '<strong>No Image Uploaded!</strong>';

    $locationAddress = $location['address_line1'].'<br>';
    $locationAddress .= ($location['address_line2'] != '') ? $location['address_line2'].'<br>' : NULL;
    $locationAddress .= ($location['address_city'] != '') ? $location['address_city'].'<br>': NULL;
    $locationAddress .= $location['address_county'].'<br>';
    $locationAddress .= $location['address_postal_code'].'<br>';

    $outputTr .= '
      <tr id="'.$location['id'].'">
        <td width="100px">'.$editBtn.$toggleBtn.'</td>
        <td>'.$ptImage.'</td>
        <td>'.$location['name'].'</td>
        <td>'.$locationAddress.$premisesCode.'</td>
        <td>'.$visible.'</td>
      </tr>';
  }

标签: php

解决方案


推荐阅读