0

我的代码总是进入“else-part”,即使应该返回“if-part”。当我删除“else-part”时,它会返回“if-part”。

  foreach ($messageRepo as $oMessage) {
    $messageTag = $oMessage->getMessageTags(); // get tags from all messages   
    $storeTagsUnseri = unserialize($messageTag); // unserialize tags given by the user
    $storeTagsUnseri = (array)($storeTagsUnseri);        

    if (array_intersect($messageTagsArray,$storeTagsUnseri)) {  //if tags from user matches tags from DB messages
      $messageToTag[] = $oMessage->getMessageText();     // get the message text from the DB message and store it in array
      $messageCrdate[] = $oMessage->getCrdate();

      $allMessagesArray = array_flip(array_combine($messageToTag,$messageCrdate));
      return $allMessagesArray;
    } else {
      $allMessagesArray[] = "flummi";
      return $allMessagesArray;
    }    
  }

即使 array_intersect 为真,此代码也始终返回 $allMessageArray 0 => "flummi"。

也许 if(array_intersect) 根本不可能?是否有另一种方法来检查一个数组中的任何内容是否与另一个数组中的匹配?

谢谢你的帮助!

编辑

      foreach ($messageRepo as $oMessage) {
    $messageTag = $oMessage->getMessageTags(); // get tags from all messages   
    $storeTagsUnseri = unserialize($messageTag); // unserialize tags given by the user
    $storeTagsUnseri = (array)($storeTagsUnseri);        

    if (array_intersect($messageTagsArray,$storeTagsUnseri)) {  //if tags from user matches tags from DB messages
      $messageToTag[] = $oMessage->getMessageText();     // get the message text from the DB message and store it in array
      $messageCrdate[] = $oMessage->getCrdate();

      $allMessagesArray = array_flip(array_combine($messageToTag,$messageCrdate)); 
    }      
  }

  if (!empty($allMessagesArray)) {
      return $allMessagesArray;
  } else {
      return $allMessagesArray[] = "error";
  }


}

这现在有效......但我猜不是很好的编码。^^

4

0 回答 0