Php 正在修复“警告:非法字符串偏移”-(但不会丢失内容)

Php 正在修复“警告:非法字符串偏移”-(但不会丢失内容),php,web-services,codeigniter,simplexml,Php,Web Services,Codeigniter,Simplexml,我一直在寻找解决这个问题的办法,但没有人能解决我的问题。 答案建议我在处理数组之前使用isset检查数组。但我以后会解释为什么它不适合我 预请求: 我从一个tour&travel Web服务中得到了一个巨大的XML文件,我将解析它并将其转换为PHP数组,然后对其执行一些操作。主要是过滤旅游 我的做法: 我使用SimpleXML加载xml并将其转换为PHP数组,如下所示: 然后我将这个数组连同搜索参数cityName&dates和数组本身一起发送到fitlerTours$searchParams

我一直在寻找解决这个问题的办法,但没有人能解决我的问题。 答案建议我在处理数组之前使用isset检查数组。但我以后会解释为什么它不适合我

预请求: 我从一个tour&travel Web服务中得到了一个巨大的XML文件,我将解析它并将其转换为PHP数组,然后对其执行一些操作。主要是过滤旅游

我的做法: 我使用SimpleXML加载xml并将其转换为PHP数组,如下所示:

然后我将这个数组连同搜索参数cityName&dates和数组本身一起发送到fitlerTours$searchParams$tourArray方法

然后使用foreach,我将在每次巡演中寻找城市名称,如果找到,我将升起一面旗帜

问题 当我过滤包含日期的cityName的旅游时,我得到了这个

Severity: Warning
Message: Illegal string offset 'year'
Filename: controllers/tourFilter.php
Line Number: 78
同时显示“月”和“日”的警告。 这是我的日期过滤器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变量在某个点被检测为字符串

字符串中的字符可以通过指定 使用字符串后所需字符的从零开始的偏移量 方括号,如$str[42]中所示。将字符串视为数组 用于此目的的字符集。

所以试试这个:

    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);
    }

您是否尝试记录$date print\r$date的内容,对吗?它可能不是数组。。。看起来错误是说它是一个字符串…结果在XML中有4-5个douchebag troll tours,它们的数组结构不好。。。谢谢,干杯!!
    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);
    }