2015-01-25 3 views
-2

Я хочу отобразить двумерный массив json. Я пишу запрос динамически в php, как показано ниже.json array fomate issue

$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" 
} 
} 
} 
] 

означает то же время в одном массиве. заранее спасибо

+2

Итак, что вы пытались решить эту проблему? Что не работает с этим решением? – Bowdzone

+0

Покажите свой '$ select_query'. –

+0

выберите s. *, St.sch_name из ds_schedule s left join ds_schedule_type st on st.id = s.schtype_id –

ответ

0

это было немного трудно проанализировать, что вывод, который вы хотите иметь ... если я понимаю правильно, это может помочь вам:

$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" 
     } 
    ] 
} 
+0

спасибо @bohrsty, я получил точный ответ, он действительно очень мне помогает –