gpt4 book ai didi

php - 在js中推送数组看起来无法正常工作

转载 作者:行者123 更新时间:2023-11-28 09:36:19 25 4
gpt4 key购买 nike

 $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',
)
)
)
),
);

我从 Controller 中获取了这个数组,该数组被传递到我的 Twig ,我在其中执行以下代码将其解析为 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 被视为一个对象而不是航类的属性。

最佳答案

您需要以不同的方式构建“飞行”对象;确实不需要“$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 %}

关于php - 在js中推送数组看起来无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13002686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com