array_intersect总是计算为false

时间:2013-01-08 09:37:20

标签: php arrays if-statement intersect

我的代码总是进入“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;
    }    
  }

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

也许如果(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";
  }


}

现在可以正常工作......但我认为编码不是很好。 ^^

0 个答案:

没有答案