我在这个Web服务中创建了一个Web服务我需要在一个数组中生成两个数组。一个数组通过数据库和第二个数组来计算部分,所以这两个数组我想在一个数组下需要
我的代码是:
<?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 :(得分:0)
这里我认为你需要替换
array_push($response,$response1);
与
$response = array_merge($response,$response1);
因为array_push()用于在向数组附加数组时将元素追加到数组。 所以如果在这里,如果要使用array_push()那么你必须遍历一个数组的所有元素,并通过array_push()逐个添加到另一个数组。 所以最好更好地使用array_merge()。