简体   繁体   English

PHP JSON数组格式

[英]PHP JSON Array Formatting

Let me first start off, sorry for the confusing title. 让我先开始,对不起这个令人困惑的标题。 I didn't know how to exactly describe it but here goes. 我不知道如何准确地描述它,但是这里有。 So I am querying a database for string. 所以我正在查询数据库中的字符串。 If there is only 1 result found then it is relatively easy to create an array, fill it with information, encode JSON and return that. 如果仅找到1个结果,则创建一个数组,将其填充信息,编码JSON并返回该数组相对容易。 I am confused as to when there are multiple results. 我对何时有多个结果感到困惑。 The code below is what I am using but I highly doubt it is correct. 下面的代码是我正在使用的代码,但我高度怀疑它是正确的。 I can't encode it into JSON format using my method which is what I need. 我无法使用我需要的方法将其编码为JSON格式。 If you can help at least point me in the correct direction, I would be more than grateful! 如果您至少可以帮助我指出正确的方向,我将不胜感激! Thank you! 谢谢!


PHP: PHP:

if ($action == 'profile') {
    while ($pson = mysql_fetch_array($personQuery)) {
        $typeSearch = 'profile';
        $profQuery = mysql_query("SELECT * FROM tableName WHERE ColumnName LIKE '$query'");
        $compQuery = mysql_query("SELECT * FROM tableName2 WHERE ColumnName LIKE '$query'");
        if ($profQuery && mysql_num_rows($profQuery) > 0) {
            $personQueryRows = mysql_num_rows($profQuery);
            while ($row = mysql_fetch_array($profQuery)) {
                if ($compQuery && mysql_num_rows($compQuery) > 0) {
                    while ($com = mysql_fetch_array($compQuery)) {
                        if (mysql_num_rows($profQuery) > 1) { 
                            $compQueryRows = mysql_num_rows($compQuery);
                            if ($compQueryRows > 0) {
                                $compReturn = "true";
                            } else {
                                $compReturn = "false";
                            }
                            $nameArray = Array(
                                "success"=>"true",
                                "date"=>date(),
                                "time"=>$time,
                                "action"=>$action,
                                "returned"=>"true"
                            );
                            global $result;
                            for ($i=1;$i<=$personQueryRows;$i++) {
                                $nameResult[$i]=Array(
                                    "id"=>$row['id'],
                                    "name"=>$row['name'],
                                    "gender"=>$row['gender'],
                                    "comp"=>$row['company'],
                                    "queryType"=>"profile"
                                );
                                $result = array_merge($nameArray, $nameResult[$i]);
                            }
                            $encodedJSON = json_encode($result);
                            echo $encodedJSON;
                        }
                    }
                }
            }
        }
    }
}

} }

Returned JSON: 返回的JSON:

{"success":"true","date":"Jun 29 2012","time":"14:43:16","action":"profile","returned":"true","id":"14321","name":"John Smith","gender":"male","comp":"ABC Studios, LLC.","queryType":"profile"}
{"success":"true","date":"Jun 29 2012","time":"14:43:16","action":"profile","returned":"true","id":"292742","name":"John Smith","gender":"male","comp":"DEF Studios, LLC.","queryType":"profile"}

JavaScript error (when parsing JSON): JavaScript错误(解析JSON时):

Uncaught SyntaxError: Unexpected token { 

PS I am just getting started with PHP Arrays, and JSON formatting so I apologize if this is totally wrong. PS我刚开始使用PHP数组和JSON格式,所以如果这是完全错误的话,我深表歉意。 Still in the learning phase. 仍处于学习阶段。

It looks like you're building up $nameResult[$i], but then you do: 看起来您正在建立$ nameResult [$ i],但随后您执行了以下操作:

$result = array_merge($nameArray, $nameResult[$i]);

You're doing that in each iteration of that for loop (once for each of the rows you got back), meaning that each time, you're clobbering $result. 您在for循环的每次迭代中都这样做(对于返回的每一行一次),这意味着每次您都在浪费$ result。

After you finish that for loop, you then take whatever $result finally is (meaning the last $personQueryRows), and then json_encode it. 在完成for循环之后,您需要获取最终的$ result(意味着最后的$ personQueryRows),然后对其进行json_encode。

Looking at your other question (http://stackoverflow.com/questions/11257490/jquery-parse-multidimensional-array), it looks like what you should really be doing is before the loop where you go over $personQueryRows: 查看您的其他问题(http://stackoverflow.com/questions/11257490/jquery-parse-multiDimension-array),看来您真正应该做的是在遍历$ personQueryRows的循环之前:

$output=$nameArray;

And then replace the array_merge line with: 然后将array_merge行替换为:

$output[] = $nameResult[$i];

That last line will append the $result array onto the $output array as a new array member, meaning that it's nesting the array, which is what you'll need for your nested JSON. 最后一行会将$ result数组作为新的数组成员追加到$ output数组上,这意味着它将嵌套该数组,这就是嵌套JSON所需要的。

Your code should look like this: 您的代码应如下所示:

global $result;
$result = array();
.......    

if ($action == 'profile') {
  while{{{{{{{{...}}}}}}}}}}}

  $encodedJSON = json_encode( $result );
  echo $encodedJSON;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM