我有一些表单数据,我使用jQuery以JSON格式保存(这一切都正常)。
但是我想要获取JSON数据并将其显示在PHP for循环中。这是我遇到麻烦的地方,因为jQuery的serializeArray和JSON.stringify只是将每个表单值添加到一个比特块中,例如。
check in, check out, room, adults, children, check in, check out, room, adults, children, check in, check out, room, adults, children
但我想把每个房间分开,例如
(Room 1) check in, check out, room, adults, children
(Room 2) check in, check out, room, adults, children
(Room 3) check in, check out, room, adults, children
JSFiddle表示如何创建表单数据(一切正常):https://jsfiddle.net/kuohhm2q/
PHP我正在尝试在for循环中尝试显示JSON数据(不工作):
<?php
$json_data = '[{"name":"check_in","value":"07/11/2017"},{"name":"check_out","value":"07/26/2017"},{"name":"room_type","value":"option_1"},{"name":"adults","value":"2"},{"name":"children","value":"3"},{"name":"check_in","value":"07/27/2017"},{"name":"check_out","value":"07/29/2017"},{"name":"room_type","value":"option_2"},{"name":"adults","value":"3"},{"name":"children","value":"2"}]';
$data = json_decode($json_data, true);
//print_r($data);
foreach($data as $key => $val) { ?>
<?php echo $key; ?>
<?php } ?>
答案 0 :(得分:0)
您应该使用$data as $key
和echo $key['name']
<?php
$json_data = '[{"name":"check_in","value":"07/11/2017"},{"name":"check_out","value":"07/26/2017"},{"name":"room_type","value":"option_1"},{"name":"adults","value":"2"},{"name":"children","value":"3"},{"name":"check_in","value":"07/27/2017"},{"name":"check_out","value":"07/29/2017"},{"name":"room_type","value":"option_2"},{"name":"adults","value":"3"},{"name":"children","value":"2"}]';
$data = json_decode($json_data, true);
// echo "<pre>";
// print_r($data);
$i=0;
foreach($data as $key) {
if($i==2) break;
echo 'Name: '.$key['name'];
echo "<br>";
echo 'Value: '.$key['value'];
echo "<br>";
$i++;
}
?>
答案 1 :(得分:0)
问题在于您如何构建JSON。由于在表单上使用serializeArray()
,每个项目都有自己的对象,这意味着您不知道什么属性适用于哪个房间。
要解决此问题,请使用map()
来构建每个房间的对象数组。像这样:
function save_all_rooms() {
$(".save_all_rooms").on('click', function(e) {
var formData = $('.add_new_form .the_form').map(function() {
return {
check_in: $(this).find('[name="check_in"]').val(),
check_out: $(this).find('[name="check_out"]').val(),
room_type: $(this).find('[name="room_type"]').val(),
adults: $(this).find('[name="adults"]').val(),
children: $(this).find('[name="children"]').val()
}
}).get();
console.log(formData);
});
}
结果将采用以下格式,其中每个对象都包含房间的属性。然后可以轻松地迭代:
[{
"check_in": "07/04/2017",
"check_out": "07/10/2017",
"room_type": "option_1",
"adults": "1",
"children": "1"
}, {
"check_in": "07/28/2017",
"check_out": "07/31/2017",
"room_type": "option_3",
"adults": "3",
"children": "3"
}]
答案 2 :(得分:0)
我认为这应该有用
<?php
$json_data = '[{"name":"check_in","value":"07/11/2017"},{"name":"check_out","value":"07/26/2017"},{"name":"room_type","value":"option_1"},{"name":"adults","value":"2"},{"name":"children","value":"3"},{"name":"check_in","value":"07/27/2017"},{"name":"check_out","value":"07/29/2017"},{"name":"room_type","value":"option_2"},{"name":"adults","value":"3"},{"name":"children","value":"2"}]';
$data = json_decode($json_data, true);
//print_r($data);
foreach($data as $key => $val) {
foreach($val as $val_key=>$val_val) {
echo $val_key.":".$val_val."</br>";
}
}
?>
答案 3 :(得分:0)
使用json_decode读取json值,您可以使用array_slice来保留前两个元素。试试这个:
<?php
$json_data = '[{"name":"check_in","value":"07/11/2017"},{"name":"check_out","value":"07/26/2017"},{"name":"room_type","value":"option_1"},{"name":"adults","value":"2"},{"name":"children","value":"3"},{"name":"check_in","value":"07/27/2017"},{"name":"check_out","value":"07/29/2017"},{"name":"room_type","value":"option_2"},{"name":"adults","value":"3"},{"name":"children","value":"2"}]';
$data = json_decode($json_data);
foreach(array_slice($data,0,2) as $key => $val) {
echo $val->name."-".$val->value."<br>";
} ?>