2017-03-21 147 views
0

我正在這個Web服務中創建一個Web服務我需要兩個數組結果在一個數組中。一個陣列來通過出庫和第二陣列來進行計算一部分,所以這個兩陣我想一個數組下需要array_push方法不起作用

我的代碼是:

<?php 
$jsonData = file_get_contents("php://input"); 
$phpArray = json_decode($jsonData, true); 

include 'connection.php'; 

$pc_name  = $_POST["pc_name"]; 
$total_hours = $_POST["total_hours"]; 
$base_fare  = $_POST["base_fare"]; 
$start_date  = $_POST["start_date"]; 
$start_month = date("M", strtotime($start_date)); 
$end_date  = $_POST["end_date"]; 
$end_month  = date("M", strtotime($end_date)); 
$weekday_status = $_POST["weekday_status"]; 
$v_model  = $_POST["v_model"]; 
$day_status  = $_POST["day_status"]; 
$date   = date("m/d/Y"); 
//print_r("select * from tbl_promocode where promocode='$pc_name'"); die(); 
    $sql  = "select * from tbl_promocode where promocode='$pc_name'"; 
    $stmt  = sqlsrv_query($conn, $sql); 
    $checkrow = sqlsrv_has_rows($stmt); 
    if($checkrow>0) 
    { 
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) 
    { 
     $cat_type = $row['cat_type']; 
     $end_date = $row['end_date']; 
    } 
    if($date>$end_date) 
     { 
      $response["response"]="Offer is expired"; 
     } 
     else 
     { 
      if($cat_type==1) 
      { 
       $sql1  = "select * from tbl_promocode where cat_type='1' and promocode='$pc_name'"; 
       $stmt1  = sqlsrv_query($conn, $sql1); 
       while($row1 = sqlsrv_fetch_array($stmt1, SQLSRV_FETCH_ASSOC)) 
       { 
        $result = $row1; 
       } 
       $response["response"]="success"; 
       $response["result"]=$result;    
      } 
      else if($cat_type==2) 
      { 
       if($total_hours>=4) 
       { 
        $sql2  = "select * from tbl_promocode where cat_type='2' and promocode='$pc_name'"; 
        $stmt2  = sqlsrv_query($conn, $sql2); 
        while($row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) 
        { 
         $result2 = $row2; 
        } 
        $response["response"]="success"; 
        $response["result"]=$result2; 
       } 
            else 
       { 
        $response["response"]="Invalid Promocode"; 
       } 
      } 
      else if($cat_type==3) 
      { 
       if($total_hours>=48) 
       { 
        $sql3  = "select * from tbl_promocode where cat_type='3' and promocode='$pc_name'"; 
        $stmt3  = sqlsrv_query($conn, $sql3); 
        while($row3 = sqlsrv_fetch_array($stmt3, SQLSRV_FETCH_ASSOC)) 
        { 
         $result3 = $row3; 
        } 
        $response["response"]="success"; 
        $response["result"]=$result3; 
       } 
           else 
       { 
        $response["response"]="Invalid Promocode"; 
       } 
      } 
      else if($cat_type==4) 
      { 
       if($base_fare>=5000) 
       { 
        $sql4  = "select * from tbl_promocode where cat_type='4' and promocode='$pc_name'"; 
        $stmt4  = sqlsrv_query($conn, $sql4); 
        while($row4 = sqlsrv_fetch_array($stmt4, SQLSRV_FETCH_ASSOC)) 
        { 
         $result4 = $row4; 
        } 
        $response["response"]="success"; 
        $response["result"]=$result4; 
       } 
          else 
       { 
        $response["response"]="Invalid Promocode"; 
       } 
      } 
      else if($cat_type==5) 
      { 
        $sql5  = "select * from tbl_promocode where cat_type='5' and promocode='$pc_name'"; 
        $stmt5  = sqlsrv_query($conn, $sql5); 
        while($row5 = sqlsrv_fetch_array($stmt5, SQLSRV_FETCH_ASSOC)) 
        { 
         $result5 = $row5; 
        } 
        $amount  = 10/100*$base_fare; 
        $response["response"]="success"; 
        $response["result"]=$result5; 
        $response1["cal_amount"]=$amount; 
        array_push($response,$response1); 
      } 
      else if($cat_type==6) 
      { 
        $sql6  = "select * from tbl_promocode where cat_type='6' and promocode='$pc_name'"; 
        $stmt6  = sqlsrv_query($conn, $sql6); 
        while($row6 = sqlsrv_fetch_array($stmt6, SQLSRV_FETCH_ASSOC)) 
        { 
         $result6 = $row6; 
        } 
        $amount  = 20/100*$base_fare; 
        $response["response"]="success"; 
        $response["result"]=$result6; 
        $response1["cal_amount"]=$amount; 
        array_push($response,$response1); 
      } 
      else if($cat_type==7) 
      { 
        $sql7  = "select * from tbl_promocode where cat_type='7' and promocode='$pc_name'"; 
        $stmt7  = sqlsrv_query($conn, $sql7); 
        while($row7 = sqlsrv_fetch_array($stmt7, SQLSRV_FETCH_ASSOC)) 
        { 
         $result7 = $row7; 
        } 
        $amount  = 30/100*$base_fare; 
        $response["response"]="success"; 
        $response["result"]=$result7; 
        $response1["cal_amount"]=$amount; 
        array_push($response,$response1); 
      } 
      else if($cat_type==8) 
      { 
        $sql8  = "select * from tbl_promocode where cat_type='8' and promocode='$pc_name'"; 
        $stmt8  = sqlsrv_query($conn, $sql8); 
        while($row8 = sqlsrv_fetch_array($stmt8, SQLSRV_FETCH_ASSOC)) 
        { 
         $result8 = $row8; 
        } 
        $amount  = 50/100*$base_fare; 
        $response["response"]="success"; 
        $response["result"]=$result8; 
        $response1["cal_amount"]=$amount; 
        array_push($response,$response1); 
      } 
      else if($cat_type==9) 
      { 
        $sql9  = "select * from tbl_promocode where cat_type='9' and promocode='$pc_name'"; 
        $stmt9  = sqlsrv_query($conn, $sql9); 
        while($row9 = sqlsrv_fetch_array($stmt9, SQLSRV_FETCH_ASSOC)) 
        { 
         $result9 = $row9; 
        } 
        $amount  = 10/100*$base_fare; 
        $response["response"]="success"; 
        $response["result"]=$result9; 
        $response1["cal_amount"]=$amount;     
        array_push($response,$response1); 
      } 
      else 
      { 
       $response["response"]="Invalid Promocode"; 
      } 
     } 
    } 
else 
    { 
     $response["response"]="Invalid Promocode"; 
    } 
die(json_encode($response)); 
?> 

my result is:- 

{ 
    "0": { 
    "cal_amount": 500 
    }, 
    "response": "success", 
    "result": { 
    "id": 6, 
    "promocode_name": "TRIP10", 
    "start_date": { 
     "date": "2017-03-20 00:00:00.000000", 
     "timezone_type": 3, 
     "timezone": "UTC" 
    }, 
    "end_date": { 
     "date": "2017-05-31 00:00:00.000000", 
     "timezone_type": 3, 
     "timezone": "UTC" 
    }, 
    "promocode": "TRIP10", 
    "amount": "empty", 
    "amount_type": "empty", 
    "status": "active", 
    "Cancel_Flag": 0, 
    "cat_type": 5, 
    "percentage": "10", 
    "v_model": "empty", 
    "off_month": "empty" 
    } 
} 

使用該Web服務我的數據是正確的,但問題是在結果集中開始零打印。我想刪除這個零或轉換爲任何字符串。所以,我做什麼來解決這個問題,請幫助我的人

+0

請將這段代碼縮減爲一個展示您的問題的最小示例。謝謝。 – arkascha

+1

我可以告訴你有語言障礙,但是你能展示你得到的和你期望的結果的例子嗎?因爲我無法理解這個問題。 – Shawn

回答

0

在這裏,我想你需要更換

array_push($response,$response1); 

$response = array_merge($response,$response1); 

因爲array_push()用於追加到數組中的元素而你正在追加一個數組。 所以如果在這裏如果要使用array_push(),那麼你必須遍歷一個數組的所有元素,並通過array_push()逐個添加到另一個數組中。 所以爲了更好地使用array_merge()來代替。