2015-04-24 120 views
1

我已經爲此問題尋找解決方案,但沒有解決我的問題。 答案建議我在使用isset之前檢查數組。但我會解釋它以後如何做不到這一點。修復「警告:非法字符串偏移量」 - (但不會丟失內容)

預REQ:
我已經從一個旅遊&旅行web服務,我會分析並轉換爲PHP數組,後來做一些關於它的操作得到了巨大的XML文件。 (主要是過濾遊覽)。

我的方法:
我使用的SimpleXML加載XML並將其轉換成PHP數組,像這樣:

$xml = file_get_contents(APPPATH."tour.xml", true); 
$xmlString = htmlentity_to_xml($xml); //custom method to clean XML 
$Str = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA); 

//converting to array 
$json = json_encode($Str); 
$array = json_decode($json,TRUE); 

然後我一起發送這個數組的fitlerTours($searchParams, $tourArray)方法搜索參數(城市名稱&日期)和數組本身。

然後使用foreach()我正在經歷每次巡視以尋找cityName並且如果找到了標誌。

問題
當我過濾包含cityName的遊覽日期時,我得到了這個。

Severity: Warning 
Message: Illegal string offset 'year' 
Filename: controllers/tourFilter.php 
Line Number: 78 

警告顯示forfeset'月'和'日'也。
這裏是我的PHP的日期過濾器:(78號線4號線)

if($flag == 1){ 
    if(!empty($fromDate)){ 
    foreach($tour['departureDates']['date'] AS $date){ 
     $dateDep = strtotime($date['year'] . "-" . (($date['month']) < 10 ? "0".$date['month'] : $date['month']) . "-" . (($date['day']) < 10 ? "0".$date['day'] : $date['day'])); 
     if(strtotime($fromDate) <= $dateDep && $dateDep <= strtotime($fromDate . "+".$range." days")){ 
      if($date['departureStatus'] != "SoldOut"){ 
      $dateFlag = 1; 
      } 
     } 
    } 
    } 
    else{ 
    $dateFlag = 1; 
    } 
    $flag = 0; 
} 

if($dateFlag == 1){//Collect tours which contain the keyword & dates to $response array 
    $responseArray[] = $tour; 
    $dateFlag = false; //Reset Flag 
} 

這裏是XML的片段:

... 
<departureDates> 
     <date> 
      <day>7</day> 
      <month>1</month> 
      <year>2016</year> 
      <singlesPrice>12761</singlesPrice> 
      <doublesPrice>9990</doublesPrice> 
      <triplesPrice>0</triplesPrice> 
      <quadsPrice>0</quadsPrice> 
      <shipName/> 
      <departureStatus>Available</departureStatus> 
     </date> 
     <date> 
      <day>8</day> 
      <month>1</month> 
      <year>2016</year> 
      <singlesPrice>12761</singlesPrice> 
      <doublesPrice>9990</doublesPrice> 
      <triplesPrice>0</triplesPrice> 
      <quadsPrice>0</quadsPrice> 
      <shipName/> 
      <departureStatus>SoldOut</departureStatus> 
     </date> 
</departureDates> 
... 

現在,如果我使用,我發現通過搜索周圍被檢查的解決方案該陣列由isset()正確設置,它不返回true,並且第78行不執行,數據丟失。但我需要這些數據。

這隻發生在我搜索的關鍵字上。
任何幫助表示讚賞。

+1

您是否嘗試登錄'$ date'的內容('的print_r($日期,真)')?它可能不是一個數組......它看起來像錯誤說它是一個字符串... – Random

回答

0

錯誤說,$date變種被檢測爲在某些點串...

串內

性狀可以通過指定 進行訪問和修改零基於該字符串後的期望的字符的偏移使用 方形數組括號,如$ str [42]中所示。將字符串想象爲用於此目的的字符數組 。 See here

那麼試試這個:

if(is_array($date)){ 
     $dateDep = strtotime($date['year'] . "-" . (($date['month']) < 10 ? "0".$date['month'] : $date['month']) . "-" . (($date['day']) < 10 ? "0".$date['day'] : $date['day'])); 
     if(strtotime($fromDate) <= $dateDep && $dateDep <= strtotime($fromDate . "+".$range." days")){ 
      if($date['departureStatus'] != "SoldOut"){ 
      $dateFlag = 1; 
      } 
     } 
    } 
    else { 
     //If this is not an array what is it then? 
     var_dump($date); 
    } 
+0

原來這裏有4-5個douchebag巨魔之旅,那個XML陣列結構不好......感謝和歡呼! – eNeMetcH

相關問題