1

我有两个表,我想将它们转换为 json,如下所示:

[
   {
      "date":"2013-07-20",
      "id":"123456",
      "year":"2013",
      "people":[
         {
            "name":"First",
            "age":"60",
            "city":"1"
         },
         {
            "name":"second",
            "age":"40",
            "city":"2"
         },
         {
            "name":"third",
            "age":"36",
            "city":"1"
         }
      ]
   }
]

但我的代码的结果是这样的:

[
   {
      "date":"2013-07-20",
      "id":"123456",
      "year":"2013",}
      ,{
      "people":[
         {
            "name":"First",
            "age":"60",
            "city":"1"
         },
         {
            "name":"second",
            "age":"40",
            "city":"2"
         },
         {
            "name":"third",
            "age":"36",
            "city":"1"
         }
      ]
   }
]

代码为数组“people”创建了一个新对象,我希望它在同一个对象中

$result = mysql_query("SELECT * FROM data where id='123456'");
$fetch = mysql_query("SELECT name,age,city FROM people where id='123456'"); 

$json = array();
$json2['people'] = array();

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $json[] = $row;
  }

  while ($row = mysql_fetch_assoc($fetch)){
    $row_temp["name"]=$row["name"];
    $row_temp["age"] = $row["age"];
    $row_temp["city"] = $row["city"];

   array_push($json2['people'],$row_temp);
   }

    array_push($json, $json2);

echo Json_encode($json);

如何使数组与表“数据”在同一个对象中?

非常感谢

4

3 回答 3

3

我想你可以试试这个

$result = mysql_query("SELECT * FROM data where id='123456'");
$fetch = mysql_query("SELECT name,age,city FROM people where id='123456'"); 

// I think, you'll get a single row, so no need to loop
$json = mysql_fetch_array($result, MYSQL_ASSOC);

$json2 = array();
while ($row = mysql_fetch_assoc($fetch)){
    $json2[] = array( 
        'name' => $row["name"],
        'age' => $row["age"],
        'city' => $row["city"]
    );
}
$json['people'] = $json2;
echo json_encode($json);

结果print_r($json)应该是这样的

Array
(
    [date] => 2013-07-20
    [year] => 2013
    [id] => 123456
    [people] => Array
        (
            [0] => Array
                (
                    [name] => First
                    [age] => 60
                    [city] => 1
                )

            [1] => Array
                (
                    [name] => second
                    [age] => 40
                    [city] => 2
                )

        )

)

结果echo json_encode($json)应该是

{
    "date" : "2013-07-20",
    "year":"2013",
    "id":"123456",
    "people":
    [
        {
            "name" : "First",
            "age" : "60",
            "city" : "1"
        },
        {
            "name" : "second",
            "age" : "40",
            "city" : "2"
        }
    ]
}

如果你这样做了,echo json_encode(array($json))那么你会把你的整个json包裹在一个数组中,就像这样

[
    {
        "date" : "2013-07-20",
        "year":"2013",
        "id":"123456",
        "people":
        [
            {
                "name" : "First",
                "age" : "60",
                "city" : "1"
            },
            {
                "name" : "second",
                "age" : "40",
                "city" : "2"
            }
        ]
    }
]
于 2013-07-28T19:11:35.313 回答
0

people您可以通过等待使用密钥直到加入两个数组的最后一刻来使其工作。在那之前,只需将数据加载到$jsonand$json2中。

$json = array('date' => '2013', 'id' => '123456', 'year' => '2013');

$result = mysql_query("SELECT * FROM data where id='123456'");
$fetch = mysql_query("SELECT name,age,city FROM people where id='123456'"); 

$json = array();
$json2 = array();

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $json[] = $row;
}

while ($row = mysql_fetch_assoc($fetch)){
    $row_temp["name"]=$row["name"];
    $row_temp["age"] = $row["age"];
    $row_temp["city"] = $row["city"];
    array_push($json2, $row_temp);
}

$json['people'] = $json2;

echo Json_encode($json);
于 2013-07-28T19:13:16.287 回答
0

您非常接近,但您希望 People 数组是外部数组的直接值,并且您已将其包装在一个额外的数组中。

另外,请注意您使用的 MySQL 库已弃用。这意味着它将在未来的版本中从 PHP 中删除。您应该使用mysqlipdo替换来自 MySQL_* 系列函数的调用

$result = mysql_query("SELECT * FROM data where id='123456'");
$fetch = mysql_query("SELECT name,age,city FROM people where id='123456'"); 

$json = array();

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $json[] = $row;
  }

$json['people'] = array();

  while ($row = mysql_fetch_assoc($fetch)){
    $row_temp["name"]=$row["name"];
    $row_temp["age"] = $row["age"];
    $row_temp["city"] = $row["city"];

   array_push($json['people'],$row_temp);
   }

echo Json_encode($json);
于 2013-07-28T18:52:18.770 回答