修复 "Warning: Illegal string offset" --(但不丢失内容)
Fixing "Warning: Illegal string offset" -- (but without losing content)
I have searched for solution for this problem but none fix my problem.
The answers suggest that I use isset
to check the array before working on it. But I will explain how it doesnt do it for me later.
先决条件:
我有一个来自旅游网络服务的巨大 XML 文件,我将解析并转换为 PHP 数组,然后对其进行一些操作。 (主要是过滤游览)。
我的做法:
我正在使用 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
警告显示偏移 'month' 和 'day'。
这是我的 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()
正确设置,它不 return 正确并且第 78 行未执行并且数据是丢失。但是我需要数据。
只有我搜索的关键字会出现这种情况。
任何帮助表示赞赏。
错误表明 $date
var 在某些时候被检测为字符串...
Characters within strings may be accessed and modified by specifying
the zero-based offset of the desired character after the string using
square array brackets, as in $str[42]. Think of a string as an array
of characters for this purpose.
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);
}
I have searched for solution for this problem but none fix my problem. The answers suggest that I use
isset
to check the array before working on it. But I will explain how it doesnt do it for me later.
先决条件:
我有一个来自旅游网络服务的巨大 XML 文件,我将解析并转换为 PHP 数组,然后对其进行一些操作。 (主要是过滤游览)。
我的做法:
我正在使用 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
警告显示偏移 'month' 和 'day'。
这是我的 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()
正确设置,它不 return 正确并且第 78 行未执行并且数据是丢失。但是我需要数据。
只有我搜索的关键字会出现这种情况。
任何帮助表示赞赏。
错误表明 $date
var 在某些时候被检测为字符串...
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. 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);
}