我已經爲此問題尋找解決方案,但沒有解決我的問題。 答案建議我在使用
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行不執行,數據丟失。但我需要這些數據。
這隻發生在我搜索的關鍵字上。
任何幫助表示讚賞。
您是否嘗試登錄'$ date'的內容('的print_r($日期,真)')?它可能不是一個數組......它看起來像錯誤說它是一個字符串... – Random