【问题标题】:json array fomate issuejson数组格式问题
【发布时间】:2015-01-25 10:52:03
【问题描述】:

我想显示二维 json 数组。 我在 php 中动态编写查询,如下所示。

$result = mysql_query($select_query);
$json_response = array();
$row_array=array();
while ($row = mysql_fetch_array($result)) {
    $row_array[$row['sch_from_time']]['sch_id'] = $row['id'];
    $row_array[$row['sch_from_time']]['sch_title'] = $row['sch_title'];
    $row_array[$row['sch_from_time']]['sch_date'] = $row['sch_date'];
    $row_array[$row['sch_from_time']]['sch_from_time'] = $row['sch_from_time'];
    $row_array[$row['sch_from_time']]['sch_to_time'] = $row['sch_to_time'];
    $row_array[$row['sch_from_time']]['sch_location'] = $row['sch_location'];
    $row_array[$row['sch_from_time']]['sch_latitude'] = $row['sch_latitude'];
    $row_array[$row['sch_from_time']]['sch_longitude'] = $row['sch_longitude'];
    $row_array[$row['sch_from_time']]['sch_type'] = $row['sch_name'];
    $row_array[$row['sch_from_time']]['sch_des'] =  str_replace("\"","'", $row['sch_des']);
    array_push($json_response, $row_array);
    unset($row_array);
}
print_r(json_encode($json_response));

我得到如下输出:

  [
{
10:00 AM: {
sch_id: "6",
sch_title: "sample3",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
}
},
{
10:00 AM: {
sch_id: "4",
sch_title: "sample1",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
}
},
{
02:10 AM: {
sch_id: "1",
sch_title: "Test",
sch_date: "2015-01-05",
sch_from_time: "02:10 Am",
sch_to_time: "12:05 Am",
sch_location: "Vizag",
sch_latitude: "45",
sch_longitude: "45",
sch_type: "hi",
sch_des: "Huiiiiiiiiiiiiiiiiiiiiiii"
}
}
]

但是我需要在单个键值中显示所有相同的时间,这意味着上午 10:00 有两次,所以上午 10:00 在该数组中有子数组有两个数组,如下所示

[
{
10:00 AM: {[{
sch_id: "6",
sch_title: "sample3",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
},
 {
sch_id: "4",
sch_title: "sample1",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
}]
},
{
02:10 AM: {
sch_id: "1",
sch_title: "Test",
sch_date: "2015-01-05",
sch_from_time: "02:10 Am",
sch_to_time: "12:05 Am",
sch_location: "Vizag",
sch_latitude: "45",
sch_longitude: "45",
sch_type: "hi",
sch_des: "Huiiiiiiiiiiiiiiiiiiiiiii"
}
}
}
]

表示同一时间在一个数组中。 提前谢谢

【问题讨论】:

  • 那么您尝试过什么来解决这个问题?什么不适用于该解决方案?
  • 显示你的$select_query
  • select s.*,st.sch_name from ds_schedule s left join ds_schedule_type st on st.id=s.schtype_id

标签: php mysql json


【解决方案1】:

分析你想要的输出有点困难......如果我理解正确,这可能会对你有所帮助:

$json_response = array();
while ($row = mysql_fetch_array($result)) {
    $json_response[$row['sch_from_time']][] = array(
        'sch_id' => $row['id'],
        'sch_title' => $row['sch_title'],
        'sch_date' => $row['sch_date'],
        'sch_from_time' => $row['sch_from_time'],
        'sch_to_time' => $row['sch_to_time']
        'sch_location' => $row['sch_location'],
        'sch_latitude' => $row['sch_latitude'],
        'sch_longitude' => $row['sch_longitude'],
        'sch_type' => $row['sch_name'],
        'sch_des' => str_replace("\"","'", $row['sch_des']),
    );
}
echo json_encode($json_response);

生成以下输出:

{"10:00 AM":
    [
        {"sch_id": "6",
        "sch_title": "sample3",
        "sch_date": "2015-01-25",
        "sch_from_time": "10:00 AM",
        "sch_to_time": "11:00 AM",
        "sch_location": "vizag",
        "sch_latitude": "10",
        "sch_longitude": "20",
        "sch_type": "hi",
        "sch_des": "dfas asdf dfdasf ddsfasdf asf asdf"
        },
        {"sch_id": "4",
        "sch_title": "sample1",
        "sch_date": "2015-01-25",
        "sch_from_time": "10:00 AM",
        "sch_to_time": "11:00 AM",
        "sch_location": "vizag",
        "sch_latitude": "10",
        "sch_longitude": "20",
        "sch_type": "hi",
        "sch_des": "dfas asdf dfdasf ddsfasdf asf asdf"
        }
    ],
"02:10 AM": 
    [
        {"sch_id": "1",
        "sch_title": "Test",
        "sch_date": "2015-01-05",
        "sch_from_time": "02:10 Am",
        "sch_to_time": "12:05 Am",
        "sch_location": "Vizag",
        "sch_latitude": "45",
        "sch_longitude": "45",
        "sch_type": "hi",
        "sch_des": "Huiiiiiiiiiiiiiiiiiiiiiii"
        }
    ]
}

【讨论】:

  • 谢谢@bohrsty,我得到了准确的答案,这对我帮助很大
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
相关资源
最近更新 更多