我已经为此问题寻找解决方案,但没有解决我的问题。 答案建议我在使用
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