2015-04-24 118 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

相关问题