});
</script>
<?php 
if ($this->responseArray['response'] == 'sucess') {
    if (count($this->responseArray['data']) > 0) {
        $response = $this->responseArray['data']['price'];
        if (isset($this->postData['date'])) {
            $year = substr($this->postData['date'], -4);
        } else {
            $year = '';
        }
        $depCity = $this->postData['depCity'];
        $destCity = $this->postData['destCity'];
        $newResponse = array();
        $newResCou = 0;
        $value = priceGrouping($response, 0, $newResponse, $newResCou, 0, $flexibleSearch);
        $topHeader = '';
        $innerRow = '';
        $inDateRow = '';
        if ($flexibleSearch != '') {
            $dateHeaderResultArray = createDateHeader($value);
            $inArrayLength = $dateHeaderResultArray['inArrayLength'];
            $widthDivisor = $inArrayLength > 0 ? $inArrayLength : 1;
            $dateHeaderArray = $dateHeaderResultArray['result'];
            $inflightChk = $dateHeaderResultArray['flightin'];
            $strDateHeader = '';
            $strDateRow = '';
            $str = 'date';
            if ($inflightChk != '') {
                for ($k = 0; $k < count($dateHeaderArray); $k++) {
                    $topHeader .= '<li>' . $dateHeaderArray[0][$k]["outdate"] . '</li>';
function priceGrouping($response, $i, $newResponse, $newResCou, $loopId, $flexibleChk)
{
    $attributes = $response[$i]['@attributes'];
    $bookingUrl = $attributes['bookingUrl'];
    $total = $attributes['total'];
    $outLeg = isset($response[$i]['out'][0]['leg']) ? $response[$i]['out'][0]['leg'] : $response[$i]['out']['leg'];
    $outLeg = isset($response[$i]['out'][0]['leg']) ? $response[$i]['out'][0]['leg'] : $response[$i]['out']['leg'];
    $inLeg = isset($response[$i]['in'][0]['leg']) ? $response[$i]['in'][0]['leg'] : isset($response[$i]['in']['leg']) ? $response[$i]['in']['leg'] : '';
    $inDepDate = '';
    $newInDepDate = '';
    $outDepDate = '';
    $newOutDepDate = '';
    if ($inLeg != '') {
        $inDepDate = isset($inLeg[0]['@attributes']['departureDate']) ? $inLeg[0]['@attributes']['departureDate'] : (isset($inLeg['@attributes']['departureDate']) ? $inLeg['@attributes']['departureDate'] : '');
    }
    $outDepDate = isset($outLeg[0]['@attributes']['departureDate']) ? $outLeg[0]['@attributes']['departureDate'] : (isset($outLeg['@attributes']['departureDate']) ? $outLeg['@attributes']['departureDate'] : '');
    $newResponse[$newResCou]['@attributes'] = $attributes;
    $newResponse[$newResCou]['out'][0]['leg'] = $outLeg;
    if ($inLeg != '') {
        $newResponse[$newResCou]['in'][0]['leg'] = $inLeg;
    }
    for ($j = 1; $j < count($response) & $loopId < count($response); $j++) {
        $loopId = $i + 1;
        $nextAttributes = isset($response[$loopId]['@attributes']) ? $response[$loopId]['@attributes'] : '';
        if ($nextAttributes != '') {
            $nextTotal = $nextAttributes['total'];
            $nextitineraryId = $nextAttributes['itineraryId'];
            $newOutLeg = isset($response[$loopId]['out'][0]['leg']) ? $response[$loopId]['out'][0]['leg'] : (isset($response[$loopId]['out']['leg']) ? $response[$loopId]['out']['leg'] : '');
            $newOutDepDate = isset($newOutLeg[0]['@attributes']['departureDate']) ? $newOutLeg[0]['@attributes']['departureDate'] : (isset($newOutLeg['@attributes']['departureDate']) ? $newOutLeg['@attributes']['departureDate'] : '');
            if ($inLeg != '') {
                $newInLeg = isset($response[$loopId]['in'][0]['leg']) ? $response[$loopId]['in'][0]['leg'] : (isset($response[$loopId]['in']['leg']) ? $response[$loopId]['in']['leg'] : '');
                $newInDepDate = isset($newInLeg[0]['@attributes']['departureDate']) ? $newInLeg[0]['@attributes']['departureDate'] : (isset($newInLeg['@attributes']['departureDate']) ? $newInLeg['@attributes']['departureDate'] : '');
            }
            $conditionStr = '';
            $c1 = '';
            $c2 = '';
            if ($inLeg != '' && $flexibleChk == 1) {
                $c1 = $inDepDate;
                $c2 = $newInDepDate;
            } else {
                if ($flexibleChk == 1) {
                    $c1 = $outDepDate;
                    $c2 = $newOutDepDate;
                }
            }
            if ($nextTotal == $total && $c1 == $c2) {
                $newResponse[$newResCou]['out'][$j]['itineraryId'] = $nextitineraryId;
                if ($j == 1) {
                    $currentOutLeg = $outLeg;
                    $nextOutLeg = $newOutLeg;
                    if ($inLeg != '') {
                        $currentInLeg = $inLeg;
                        $nextInLeg = $newInLeg;
                    }
                } else {
                    $currentOutLeg = $newOutLeg;
                    $nextOutLeg = isset($response[$loopId - 1]['out'][0]['leg']) ? $response[$loopId - 1]['out'][0]['leg'] : isset($response[$loopId - 1]['out']['leg']) ? $response[$loopId - 1]['out']['leg'] : '';
                    if ($inLeg != '') {
                        $currentInLeg = $inLeg;
                        $nextInLeg = isset($response[$loopId - 1]['in'][0]['leg']) ? $response[$loopId - 1]['in'][0]['leg'] : isset($response[$loopId - 1]['in']['leg']) ? $response[$loopId - 1]['in']['leg'] : '';
                    }
                }
                $outmatched = legComparison($currentOutLeg, $nextOutLeg);
                if ($outmatched == 0) {
                    $newResponse[$newResCou]['out'][$j]['leg'] = $newOutLeg;
                }
                //$outmatched == 0
                if ($inLeg != '') {
                    $inmatched = legComparison($currentInLeg, $nextInLeg);
                    if ($inmatched == 0) {
                        $newResponse[$newResCou]['in'][$j]['leg'] = $newInLeg;
                    }
                    //$inmatched == 0
                }
                // unset($response[$loopId]);
                $i++;
            } else {
                $newResCou = $newResCou + 1;
                $i++;
                return priceGrouping($response, $i, $newResponse, $newResCou, $loopId, $flexibleChk);
            }
        }
        //$nextAttributes != ''
        //echo "Change Loop"."##".$i."<br>";
    }
    //$j = 1; $j < count($response) & $loopId < count($response); $j++
    return $newResponse;
}