为什么我无法返回正确的json结构?

时间:2014-06-14 16:15:35

标签: php json

我正面临关于json结构的问题。

我有这个:

function loadActiveTrackers($host_l, $username_l, $password_l, $dbName_l, $companyId_l){  

                $trackerId = 0;
                $trackerName = "";
                $trackerCreator = "";
                $projectName = "";

                $dataArray = [];

                $trackerIdsArray = [];
                $trackerNamesArray = [];
                $trackerCreatorsArray = [];
                $projectNamesArray = [];                

                $mysqli = new mysqli($host_l, $username_l, $password_l, $dbName_l);

                $getActiveTrackersQuery = "SELECT tracker_id, tracker_name, tracker_creator, project_name FROM trackers WHERE "
                                     . "company_id = ? AND is_active=1 ORDER BY tracker_creation_date";

                if($stmt = $mysqli->prepare($getActiveTrackersQuery)){

                   $stmt->bind_param('s',$companyId_l);

                   $stmt->execute();                 

                   /* Store the result (to get properties) */
                   $stmt->store_result();                    

                   /* Bind the result to variables */
                   $stmt->bind_result($trackerId, $trackerName, $trackerCreator, $projectName);

                   while ($stmt->fetch()) {
                        $trackerIdsArray[] = $trackerId;
                        $trackerNamesArray[] = $trackerName;
                        $trackerCreatorsArray[] = $trackerCreator;
                        $projectNamesArray[] = $projectName;
                    }

                    $dataArray['ids'] = $trackerIdsArray;
                    $dataArray['names'] = $trackerNamesArray; 
                    $dataArray['creators'] = $trackerCreatorsArray;
                    $dataArray['projectNames'] = $projectNamesArray;


//                   print_r($trackerIdsArray);

                   /* free results */
                   $stmt->free_result();

                   /* close statement */
                   $stmt->close();
                }

                /* close connection */
                $mysqli->close();   

                echo json_encode($dataArray);
}

上面的代码工作正常,但结构无效。 我的意思是它返回:

{
    "ids": [1,2,3,4],
    "names": ["1","2","test tracker","test1"],
    "creators": ["1","test","test","test"],
    "projectNames": ["1","1","test project","test"]
}

但它应该回归:

[
   {"id": 1, "name": "1", "creator": "1", "projectName": "1"}, 
   {"id": 2, "name": "2", "creator": "test", "projectName": "1"}, 
   {"id": 3, "name": "test tracker", "creator": "test", "projectName": "test project"}, 
   {"id": 4, "name": "test1", "creator": "test", "projectName": "test"}
]
你能引导我吗?我知道它的确很小,但是我不能把它当成一个php新手。在java中,json库非常强大,只需要一个push()即可实现,但在这里我真的无法找到方法。

1 个答案:

答案 0 :(得分:2)

当前代码创建了id,名称,创建者和项目名称的数组。

所以,而不是这段代码:

while ($stmt->fetch()) {
    $trackerIdsArray[] = $trackerId;
    $trackerNamesArray[] = $trackerName;
    $trackerCreatorsArray[] = $trackerCreator;
    $projectNamesArray[] = $projectName;
}

$dataArray['ids'] = $trackerIdsArray;
$dataArray['names'] = $trackerNamesArray; 
$dataArray['creators'] = $trackerCreatorsArray;
$dataArray['projectNames'] = $projectNamesArray;

将您的结构更改为:

while ($stmt->fetch()) {
    $dataArray[] = array( 'id'          => $trackerId,
                          'name'        => $trackerName,
                          'creator'     => $trackerCreator,
                          'projectName' => $projectName
                   );
}

echo json_encode($dataArray);

这将返回您想要的结构,即id,名称,创建者和项目名称的数组。