将所选行转换为JSON - PHP

时间:2014-11-10 22:03:42

标签: php android json web-services

我想将所选结果转换为JSON。 这是我的代码:

<?php
include("DbConnect.php");
    $connection=new DbConnect();
$sth = mysqli_query($connection->_con,"SELECT * FROM account WHERE ac_id='1'");
                if($sth){
                $rows = array();
                while($row = mysqli_fetch_assoc($sth)){
                  $users = mysqli_query($connection->_con,"SELECT user.user_id,user.name,user.email,ac_detail.ac_id,ac_detail.amount FROM user,ac_detail WHERE ac_detail.ac_id='1' AND user.user_id=ac_detail.user_id");
                  $usersArray = array();
                  while($userRow = mysqli_fetch_assoc($users)){
                   $usersArray[]=$userRow;
                  }
                  $a=array("users"=>$usersArray);
                  //$row["user"]=$usersArray
                    array_push($row,$a);
                  $rows[] = $row; 
                }
                echo json_encode(array('data'=>$rows));
                }else{
                  echo json_encode(array('message'=>'error - 2'));
                }

?>

通过执行此代码,它生成JSON,如:

  

{"data":[{"ac_id":"1","user_id":"2","title":"Travel","ac_for":"Traveling","required_amount":"50","current_amount":"0","initial_date":"2014-11-11","final_date":"2014-11-14","is_shared":"1","status":"1","0":{"users":[{"user_id":"2","name":"Muhammad Imran","email":"macrotechnolgies@gmail.com","ac_id":"1","amount":"0"},{"user_id":"3","name":"Muhammad Imran","email":"macrotecholgies@gmail.com","ac_id":"1","amount":"0"}]}}]}

但我不想要“0”{“user :: ...}

应该如何(预期结果):

  

{"data":[{"ac_id":"1","user_id":"2","title":"Travel","ac_for":"Traveling","required_amount":"50","current_amount":"0","initial_date":"2014-11-11","final_date":"2014-11-14","is_shared":"1","status":"1","users":[{"user_id":"2","name":"Muhammad Imran","email":"macrotechnolgies@gmail.com","ac_id":"1","amount":"0"},{"user_id":"3","name":"Muhammad Imran","email":"macrotecholgies@gmail.com","ac_id":"1","amount":"0"}]}]}

提前致谢

2 个答案:

答案 0 :(得分:0)

你在做:

 while($row = mysqli_fetch_assoc($sth)){
    [...snip...]
    array_push($row,$a);

while行会创建一个数组$row,然后您可以使用其中的一部分来创建$a。然后,将$a BACK推送到原始$row数组。但是$row已经是一个关联数组,因此推送的$a得到键0。

由于您现在将关联数组(非数字键)与数字键控数组(推送操作)混合,PHP 必须将数字键添加到您推送的项目中:您可以没有键的数组中有一个元素。

然后,由于JS不允许实际的JS数组([])具有非数字键,因此必须将整个事物转换为对象({})。

你可能想要的更像是:

while($row = ...) {
    ... build $a ...
    array_push($row['users'], $a);

代替。

答案 1 :(得分:0)

你为什么不试着array_push($row,$a)试着跟随:

<?php
include("DbConnect.php");
$connection=new DbConnect();

$sth = mysqli_query($connection->_con,"SELECT * FROM account WHERE ac_id='1'");

if($sth){
$rows = array();
while($row = mysqli_fetch_assoc($sth)){

    $users = mysqli_query($connection->_con,"SELECT user.user_id,  user.name, user.email, ac_detail.ac_id, ac_detail.amount FROM user,ac_detail WHERE ac_detail.ac_id='1' AND user.user_id=ac_detail.user_id");
    $usersArray = array();

    while($userRow = mysqli_fetch_assoc($users)){
         $usersArray[]=$userRow;
    }

// here comes the change
  //  $a = array("users"=>$usersArray);
  //            //$row["user"]=$usersArray

  //  array_push($row,$a);

    $row['users'] = $usersArray;

    $rows[] = $row; 
}

echo json_encode(array('data'=>$rows));

}else{

  echo json_encode(array('message'=>'error - 2'));

}

这应该有效。没有样本数据来测试它。