Php 如何检查imagesid和imgs是否存在数组以避免未定义的偏移量错误?

Php 如何检查imagesid和imgs是否存在数组以避免未定义的偏移量错误?,php,arrays,exists,Php,Arrays,Exists,我想检查imagesid和imgs的数组是否存在,或者imagesid和imgs的值是否相同,并且它没有抛出注意:未定义的偏移量:错误消息 我有以下代码来获取帖子的相关图片: $sql = "SELECT * FROM multiple_image"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $r

我想检查imagesid和imgs的数组是否存在,或者imagesid和imgs的值是否相同,并且它没有抛出
注意:未定义的偏移量:
错误消息

我有以下代码来获取帖子的相关图片:

$sql = "SELECT * FROM multiple_image";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $imgs[$row['imageid']][$row['id']]= "<img width='' src='../images/".$row['name']."' >"; // array of image id's, with arrays of images inside them.
  } 

 }
$sql = "SELECT * FROM posts ";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
      $commentsToImages[$row['commentid']] = $row['imagesid']; // array of post id's to picture ids
      $comments[$row['commentid']] = $row['comment']; // array of post id's to comment text.
  } 

 }
$sql=“从多个图像中选择*”;
$result=$conn->query($sql);
如果($result->num_rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
$imgs[$row['imageid'][$row['id']]=“”;//图像id数组,其中包含图像数组。
} 
}
$sql=“从帖子中选择*”;
$result=$conn->query($sql);
如果($result->num_rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
$commentsToImages[$row['commentid']]=$row['imagesid'];//帖子id到图片id的数组
$comments[$row['commentid']=$row['comment'];//要注释文本的帖子id数组。
} 
}
此代码正在呈现包含以下内容的所有相关图像:

foreach($commentsToImages as $commentID =>$imagesID) {
    ?>
<div class='main'>
  <div class='comments'>
<?php echo $comments[$commentID]; // render the comment ?>
  </div>
  <div class='pics'>
    <?php
          foreach($imgs[$imagesID] as $img) { // foreach loop that will render all the images...
                  echo $img;
          }
    ?>
  </div>
</div>

<?php
}
?>
foreach($commentstoimagesas$commentID=>$imagesID){
?>

听起来并不是所有帖子都有图像。您可能需要确保在这行
foreach($imgs[$imagesID]as$img)执行之前存在一个
$imgs[$imagesID]
。一个解决方案是在
if(isset($imgs[$imagesID])中“包装”foreach

事实上,只是一个输入错误,但这并没有回答我的问题。我想做的只是做一个
if
语句,检查它是否没有抛出错误或缺少某个值。不,我没有得到错误。我只想用一个语句包装。是的,它只针对图像,但我将注释和图像包装到
if(isset)中($imgs[commentID])
。因此,如果键(
imagesid
)没有问题,它将显示相关imgs的命令:)我也想为注释做些什么,而不仅仅是图像。这对我很有帮助。