2012-10-21 42 views
1
$Flight = array 
      (
      '1' => array 
       (
       '5' => array 
        (
        'LFID' => '6684', 
        'DepartureDate' => '2012-10-22T12:30:00', 
        'ArrivalDate' => '2012-10-22T14:00:00', 
        'BaseFareAmtInclTax' => '343.50', 
        'ReturnFlightSegmentDetails' => 
        array 
         (
         '87' => array 
          (
          'LFID' => '6647', 
          'DepartureDate' => '2012-10-24T12:25:00', 
          'FareTypeName' => 'Change', 
          'FareTypeID' => '22', 
          'BaseFareAmtInclTax' => '198', 
         ) 
        ) 
       ), 
       '10' => array 
        (
        'LFID' => '6684', 
        'DepartureDate' => '2012-10-23T12:30:00', 
        'ArrivalDate' => '2012-10-23T14:00:00', 
        'BaseFareAmtInclTax' => '243.50', 
        'ReturnFlightSegmentDetails' => 
        array 
         (
         '150' => array 
          (
          'LFID' => '6647', 
          'DepartureDate' => '2012-10-24T12:25:00', 
          'FareTypeName' => 'Change', 
          'FareTypeID' => '22', 
          'BaseFareAmtInclTax' => '198', 
         ), 
         '153' => Array 
          (
          'LFID' => '6647', 
          'DepartureDate' => '2012-10-26T12:25:00', 
          'FareTypeName' => 'Change', 
          'FareTypeID' => '22', 
          'BaseFareAmtInclTax' => '198', 
         ) 
        ) 
       ) 
      ), 
     ); 

我從控制器至極這個數組傳遞給我的樹枝在那裏我執行下面的代碼,它解析爲一個JS數組:推在爵士看不到風度的陣列正常工作

{% for flight in flights%} 
        var $FlightSegment = new Array(); 
        $flight.push({ 
         key:"{{ flight.key }}", 
         DepartureDate:"{{flight.DepartureDate}}", 
         ArrivalDate:"{{flight.ArrivalDate}}", 
         Amount:"{{flight.BaseFareAmtInclTax}}", 

        }); 
        {% for ReturnFlightSegmentDetails in flight.ReturnFlightSegmentDetails %} 
          $FlightSegment.push({ 
           LFID :"{{ReturnFlightSegmentDetails.LFID}}", 
           DepartureDate:"{{ReturnFlightSegmentDetails.DepartureDate}}", 
           ArrivalDate:"{{ReturnFlightSegmentDetails.BaseFareAmtInclTax}}", 
           Class:"{{ReturnFlightSegmentDetails.FareTypeName}}", 
          }); 
         {% endfor %} 
         console.info($FlightSegment); 
        $flight.push({ 
         ReturnFlightSegmentDetails :$FlightSegment, 
        }); 
       {% endfor %} 

問題是,$ flight.length返回4而不是2:/的ReturnFlightSegmentDetails被認爲是像一個對象,而不是成爲一個飛行中的attibut:/

+0

那麼你是單獨推入清單。 – Pointy

+0

此外,建議讓小枝用'json_encode'做這件事似乎是一個不錯的主意。 – Pointy

+0

對不起,但我不明白哪裏是錯誤:/我在全球範圍內第二次推! oO'我將排序數據,所以這就是爲什麼我選擇解析到一個js數組,因爲它會更容易,我想然後有一個字符串與json_encode – fbh

回答

0

你需要以不同的方式打造「飛行」對象; 「$ FlightSegment」變量真的不需要。

{% for flight in flights%} 
    $flight.push({ 
     key: "{{ flight.key }}", 
     DepartureDate: "{{flight.DepartureDate}}", 
     ArrivalDate: "{{flight.ArrivalDate}}", 
     Amount: "{{flight.BaseFareAmtInclTax}}", 
     ReturnFlighSegmentDetails: [ 
      {% for ReturnFlightSegmentDetails in flight.ReturnFlightSegmentDetails %} 
      { 
       LFID :"{{ReturnFlightSegmentDetails.LFID}}", 
       DepartureDate:"{{ReturnFlightSegmentDetails.DepartureDate}}", 
       ArrivalDate:"{{ReturnFlightSegmentDetails.BaseFareAmtInclTax}}", 
       Class:"{{ReturnFlightSegmentDetails.FareTypeName}}" 
       } 
       {% if not loop.last } , {% endif } 
      {% endfor %} 
     ] 
     }); 
{% endfor %} 
+0

你做了我的一天,我很感激謝謝:)但我不知道'不會得到這個{%如果不是loop.last},{%endif}它意味着什麼? – fbh

+0

@fbh它是在那個子陣列中的對象之間添加必要的逗號*。當你在最後一次迭代時,你不需要一個逗號,因爲這對老版本的IE來說是一個問題。 「循環」變量是twig'for'循環的一個特性;它會自動提供。 – Pointy