我正在使用json数据处理android应用程序。为此,我使用php和json制作了webservices。我得到了结果,但它不适合我的应用程序。以下是我的代码:
<?php
require("config.inc.php");
$query= "SELECT * from zone";
try {
$stmt = $db->prepare($query);
$result = @$stmt->execute($query_params);
}
catch (PDOException $ex) {
$zone["success"] = 0;
$zone["message"] = "Database Error!";
die(json_encode($zone));
}
$rows = $stmt->fetchAll();
if ($rows) {
$zone["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["zone_id"]=$row["zone_id"];
$post["zone_name"]=$row["zone_name"];
$post["zone_turn"]=$row["zone_turn"];
$post["zone_charge"]=$row["zone_charge"];
$query1= "SELECT * from pincode_master where zone_id=".$row['zone_id']."";
try {
$stmt = $db->prepare($query1);
$result = @$stmt->execute($query_params);
}
catch (PDOException $ex) {
$zone["success"] = 0;
$zone["message"] = "Database Error!";
die(json_encode($zone));
}
$rows1 = $stmt->fetchAll();
if ($rows1) {
$zone["posts1"] = array();
foreach ($rows1 as $row1) {
$post1 = array();
//$post1["zone_id"]=$row1["zone_id"];
$post1["pincode_number"]=$row1["pincode_number"];
//echo $post["pincode_number"];
array_push($zone["posts1"], $post1);
}
}
array_push($zone["posts"], $post,$zone["posts1"]);
}
echo json_encode($zone);
} else {
$zone["success"] = 0;
$zone["message"] = "No Zone asssign for this day!";
}
?>
从上面的代码我得到了json数组。除了pincode编号之外,它将包含一个数组中的所有数据,因为我是从另一个表中得到的。:
{"posts[{"zone_id":"2","zone_name":"A","zone_turn":"Monday,Thrusday","zone_charge":"15"},[{"pincode_number":"390007"},{"pincode_number":"390016"},{"pincode_number":"390021"},{"pincode_number":"390023"}],{"zone_id":"3","zone_name":"B","zone_turn":"Tuesday,Friday","zone_charge":"15"},[{"pincode_number":"390001"},{"pincode_number":"390004"},{"pincode_number":"390006"},{"pincode_number":"390010"},{"pincode_number":"390018"},{"pincode_number":"390019"},{"pincode_number":"390020"},{"pincode_number":"390022"}],{"zone_id":"7","zone_name":"W","zone_turn":"Saturday,Sunday,Friday","zone_charge":"15"},[{"pincode_number":"390001"},{"pincode_number":"390004"},{"pincode_number":"390006"},{"pincode_number":"390010"},{"pincode_number":"390018"},{"pincode_number":"390019"},{"pincode_number":"390020"},{"pincode_number":"390022"}],{"zone_id":"8","zone_name":"X","zone_turn":"Wednesday,Saturday,Sunday","zone_charge":"15"},[{"pincode_number":"390001"},{"pincode_number":"390004"},{"pincode_number":"390006"},{"pincode_number":"390010"},{"pincode_number":"390018"},{"pincode_number":"390019"},{"pincode_number":"390020"},{"pincode_number":"390022"}]],"posts1":[{"pincode_number":"390001"},{"pincode_number":"390004"},{"pincode_number":"390006"},{"pincode_number":"390010"},{"pincode_number":"390018"},{"pincode_number":"390019"},{"pincode_number":"390020"},{"pincode_number":"390022"}]}
但我想要下面一个:
{"posts[{"zone_id":"2","zone_name":"A","zone_turn":"Monday,Thrusday","zone_charge":"15","pincode_number":{"390007","390016","390021"}}]}
答案 0 :(得分:1)
您不必创建像这样的新数组$zone["posts1"] = array();
来添加您的密码,您只需将密码数据附加到您现有的$ post数组中。
$post = array();
$post["zone_id"]=$row["zone_id"];
$post["zone_name"]=$row["zone_name"];
$post["zone_turn"]=$row["zone_turn"];
$post["zone_charge"]=$row["zone_charge"];
.
.
.
$rows1 = $stmt->fetchAll();
if ($rows1) {
foreach ($rows1 as $row1) {
$post["pincode_number"][] = $row1["pincode_number"];
}
}
array_push($zone["posts"], $post);
}
答案 1 :(得分:0)
只需将$post1
设为多维数组,然后在foreach之后使用array_push
。如下所示:
foreach ($rows1 as $row1) {
$post1 = array();
$post1["pincode_number"][] = $row1["pincode_number"];
}
array_push($zone["posts1"], $post1);