将来自用户输入的多维数组转换为嵌套结构


Convert a multidimensional array from user input into a nested structure

我正在尝试创建一个允许用户输入房间数据的预订表单。在房间里,用户可以添加多个成人或儿童,以及每个人的姓名和性别。

那么,表单给了我这个数组:

<>之前数组([room] =>数组([0] => 3[1] => 4[2] => 5)[成人]=>数组([0] => 1[1] => 1[2] => 2[3] => 1[4] => 2)[adult_name] => Array([0] =>成人名称[1] =>成人2名称[2] =>成人名称[3] =>成人名称[4] =>成人名称)[adult_age] =>数组([0] => 05-08-2009[1] => 03-07-1974[2] => 04-03-2006[3] => 05-05-1989[4] => 04-05-1969)[adult_gender] =>数组([0] =>女[1] =>男[2] =>女性[3] =>男[4] =>女性)[children] =>数组([0] => 1[1] => 1)[child_name] => Array([0] => Child 1名称[1] => Child 2 Name)[child_age] => Array([0] => 02-04-2006[1] => 2016-03-2008)[child_gender] => Array([0] =>女[1] =>男))之前

但是,我需要重新构建它以返回如下形式的数组:

<>之前数组([font =宋体]=> font =宋体[0] => array()[成人]=>[0] => array()[adult_name] =成人名,[adult_age] = 03-07-1974,[adult_gender] =女性,)[1] => array()[adult_name] =成人名,[adult_age] = 03-07-1974,[adult_gender] =女性,)[children] => Array([0] => array()[child_name] =成人名,[child_age] = 03-07-2000,[child_gender] =女,))),[1] => array()[成人]=>[0] => array()[adult_name] =成人名,[adult_age] = 03-07-1994,[adult_gender] =男性,)[children] =>数组([0] => array()[child_name] =孩子1的名字,[child_age] = 03-07-2002,[child_gender] =女,))[children] =>数组([1] => array()[child_name] =孩子2的名字,[child_age] = 03-07-2003,[child_gender] =男,))))))之前

这可能吗?下面是我到目前为止的代码:

$(document).ready(function(){
	$(".checkdate").click(function(e){
	var i = 1;
var access = '';
		$(".check_room").each(function(){
       $('#room_result'+i).html('<img  src="http://suntroman.be/test/images/working.gif"/>');			
		var room = $(this).val();
		var el = $(this);
		
		var checkin = $("#date1").val();
		var checkout = $("#date2").val();
		$.post('http://suntroman.be/test/check_availability.php', { room_id:room, checkin:checkin, checkout:checkout, tmp_id:i } , function(data){
        
			var json = $.parseJSON(data);
			if(json.status == 1){
				$('#room_result'+json.var).html('<span style="color:green;margin-top:15px">'+json.text+'</span>');
			access += '';
			}else{
				$('#room_result'+json.var).html('<span style="color:red">'+json.text+'</span>');
			access += '1';
			}
		
			if(access){
			$(".continue").hide();
		}else{
			$(".continue").show();
		}
		});
		i++;
         e.preventDefault();
		});
		
	
	});
	$("#booking").submit(function(e){
		var el = $(this);
		$.post('http://suntroman.be/test/booking.php', $("#booking").serialize(), function(data){		
			$("#book_result").html(data);
   close_accordion_section();
   $("#accordion-2").slideDown();
	$("#accordion-2").prev().find("i").removeClass('fi-plus').addClass('fi-minus');
		});	
	e.preventDefault();
	});	
	$("#confirm_order").submit(function(e){
		$('#confirm_result').html('<div style="border-top:1px dotted #ccc;padding:20px;text-align:center;color:#999">Please Wait<br /><img width="50" class="t15" src="http://suntroman.be/test/images/loading.gif"/></div>');
		var el = $(this);
		$.post('http://suntroman.be/test/book_confirm.php', $(this).serialize(), function(data){		
			$("#confirm_result").html(data);
    
   close_accordion_section();
   $("#accordion-3").slideDown();
	$("#accordion-3").prev().find("i").removeClass('fi-plus').addClass('fi-minus');
		});	
	e.preventDefault();
	});
	
	
	var activeopen = $('.accordion .accordion-section-title.isactive');
    $('.accordion ' + activeopen.attr('href')).show();
	function close_accordion_section() {
		$('.accordion .accordion-section-title').removeClass('isactive');
		$('.accordion .accordion-section-title').find("i").removeClass('fi-minus').addClass('fi-plus');
		$('.accordion .accordion-section-content').slideUp(300).removeClass('open');
	}
	$('.accordion-section-title').click(function(e) {
		// Grab current anchor value
		var currentAttrValue = $(this).attr('href');
		if($(e.target).is('.isactive')) {
			close_accordion_section();
		}else {
			close_accordion_section();
			// Add active class to section title
			$(this).addClass('isactive');
			$(this).find("i").removeClass('fi-plus').addClass('fi-minus');
			// Open up the hidden content panel
			$('.accordion ' + currentAttrValue).slideDown(300).addClass('open'); 
		}
		e.preventDefault();
	});
});
$(document).ready(function(){
    var max_custom      = 9; //maximum input boxes allowed
    var custom_wrapper  = $(".new_room"); //Fields wrapper
    var custom_button   = $(".addroom"); //Add button ID

    var c = 1; //initlal text box count
    $(custom_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(c < max_custom){ //max input box allowed
         c++;    //text box increment
$(".new_room").append('<tr>'
	+'<td width="33%">'
	+'<label>Room 1</label><br />'
		+'<select type="select" style="width:100%" class="check_room" name="room[]">'
		+'<option value="1">Single Room</option>'
        +'<option value="2">Double Room</option>'
        +'<option value="3">Matrimonial Room</option>'
		+'</select>'
	+'</td>'
	+'<td width="33%">'
	+'<div class="left" style="width:48.5%">'
	+'<label>Adults</label><br />'
		+'<select type="select" style="width:100%" name="adults[]">'
			+'<option value="1">1</option>'
			+'<option value="2">2</option>'
			+'<option value="3">3</option>'
			+'<option value="4">4</option>'
			+'<option value="5">5</option>'
			+'<option value="6">6</option>'
		+'</select>'
    +'</div>'
	+'<div class="left l5" style="width:48.5%">'
	+'<label>Childrens</label><br />'
		+'<select type="select" style="width:100%" name="childs[]">'
		    +'<option value="0">0</option>'
			+'<option value="1">1</option>'
			+'<option value="2">2</option>'
			+'<option value="3">3</option>'
			+'<option value="4">4</option>'
			+'<option value="5">5</option>'
			+'<option value="6">6</option>'
		+'</select>'
	+'</div>'
	+'</td>'
	+'<td width="33%"><button href="#" class="remove_room pointer left" style="margin-top: 17px;"><i class="m5 fi-trash"></i></button> <div style="margin-top:17px" class="right" id="room_result'+c+'"></div></td>'
+'</tr>');
	}	
	
	$(custom_wrapper).on("click",".remove_room", function(e){ //user click on remove text
        e.preventDefault();
        $(this).parent().parent().remove();
        c--;
    });
});	
});
/*----- Accordion -----*/
.accordion,
.accordion * {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.accordion {
  overflow: hidden;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.25);
  border-radius: 3px;
  background: #f7f7f7;
}
/*----- Section Titles -----*/
.accordion-section-title {
  width: 100%;
  padding: 5px;
  display: inline-block;
  border-bottom: 1px solid #CACACA;
  background: rgb(84, 173, 221);
  transition: all linear 0.15s;
  font-size: 1em;
  text-shadow: 0px 1px 0px #383838;
  color: #FFFFFF;
  text-decoration: none;
}
.accordion-section-title.isactive,
.accordion-section-title:hover {
  background: #FF9035;
  text-decoration: none;
  text-shadow: 0px 1px 0px #292929;
  color: #FFFFFF;
}
.accordion-section:last-child .accordion-section-title {
  border-bottom: none;
}
/*----- Section Content -----*/
.accordion-section-content {
  padding: 15px;
  display: none;
}
.left{float:left}
.right{float:right}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://momentjs.com/downloads/moment.js"></script>
<script src="http://2008.kelvinluck.com/assets/jquery/datePicker/v2/demo/scripts/jquery.datePicker.js"></script>
<div class="left accordion t10" style="width:715px;">
  <div class="accordion-section">
    <a class="accordion-section-title isactive" href="#accordion-1">Booking &raquo; Step 1<i class="right fi-minus"></i></a>
    <div id="accordion-1" class="accordion-section-content open">
      <div id="msgr"></div>
      <form id="booking" action="" method="post">
        <input type="hidden" name="pid" value="1" />
        <table width="100%">
          <tr>
            <td width="33%">
              <label>Checkin</label>
              <br />
              <input type="text" style="width:100%" class="date-pick dp-applied" id="date1" name="checkin" value="" />
            </td>
            <td width="33%">
              <label>CheckOut</label>
              <br />
              <input type="text" style="width:100%" class="date-pick dp-applied" id="date2" name="checkout" value="" />
            </td>
            <td width="33%"></td>
          </tr>
          <tr>
            <td>
              <label>Room 1</label>
              <br />
              <select type="select" style="width:100%" class="check_room" name="room[]">
                <option value="1">Single Room</option>
                <option value="2">Double Room</option>
                <option value="2">Matrimonial Room</option>
              </select>
            </td>
            <td>
              <div class="left" style="width:48.5%">
                <label>Adults</label>
                <br />
                <select type="select" style="width:100%" name="adults[]">
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                  <option value="6">6</option>
                </select>
              </div>
              <div class="left l5" style="width:48.5%">
                <label>Childrens</label>
                <br />
                <select type="select" style="width:100%" name="childs[]">
                  <option value="0">0</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                  <option value="6">6</option>
                </select>
              </div>
            </td>
            <td>
              <div id="room_result1" class="right" style="margin-top:17px;text-align:right"></div>
            </td>
          </tr>
        </table>
        <table class="new_room" width="100%"></table>
        <table width="95%">
          <tr>
            <td>
              <button type="button" class="addroom pointer p5 t5" style="margin-top: 17px;"><i class="fi-plus"></i> Add Room</button>
              <button type="button" class="pointer t5 p5 checkdate" style="margin-top: 17px;"><i class="fi-magnifying-glass"></i> Check Availability</button>
              <button type="submit" class="pointer t5 p5 continue" style="margin-top: 17px;display:none"><i class="fi-magnifying-glass"></i> Continue</button>
            </td>
            <td>
            </td>
            <td>
            </td>
          </tr>
        </table>
      </form>
    </div>
    <!--end .accordion-section-content-->
  </div>
  <!--end .accordion-section-->
  <div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-2">Booking &raquo; Step 2 <i class="right fi-plus"></i></a>
    <div id="accordion-2" class="accordion-section-content">
      <form action="" id="confirm_order" method="post">
        <div id="book_result"></div>
        <fieldset class="order_field_resp">
          <legend>Responsable Details</legend>
          <p class="p5" style="border:1px dotted #ccc;background-color:#fff;font-weight:normal">Already registered? Please <a class="link" href="#">login here</a> and we will automaticaly fill the details form.</p>
          <table width="100%" class="t10">
            <tr>
              <td width="120">Full Name</td>
              <td>
                <input style="width:100%" type="text" name="resp_name" value="" placeholder="Please enter your full name..." required/>
              </td>
            </tr>
            <tr>
              <td>Email</td>
              <td>
                <input style="width:45%" type="email" name="resp_email" value="" placeholder="youremail@domain.com" required/>
              </td>
            </tr>
            <tr>
              <td>Date of birth</td>
              <td>
                <input style="width:100%" type="text" class="age_resp" name="resp_age" data-format="DD-MM-YYYY" data-template="D MMM YYYY" value="" required/>
              </td>
        </tr>
            <tr>
              <td>Gender</td>
              <td>
                <select type="select" name="resp_gender">
                  <option value="Male">Male</option>
                  <option value="Female">Female</option>
                </select>
              </td>
            </tr>
            <tr>
              <td>Address</td>
              <td>
                <input style="width:100%" type="text" name="resp_address" value="" placeholder="Please enter your full address..." required/>
            </tr>
            <tr>
              <td>Zip/Postal Code</td>
              <td>
                <input style="width:25%" type="text" name="resp_zip" value="" required/>
              </td>
            </tr>
            <tr>
              <td>Phone Nr.</td>
              <td>
                <input style="width:45%" type="number" name="resp_phone" value="" required/>
              </td>
            </tr>
            <tr>
              <td>Fax Nr.</td>
              <td>
                <input style="width:45%" type="number" name="resp_fax" value="" />
              </td>
            </tr>
            <tr>
              <td>Notes</td>
              <td>
                <textarea style="width:100%" type="textarea" name="resp_notes" value="" placeholder="Please write some extra informations if any."></textarea>
              </td>
            </tr>
          </table>
        </fieldset>
        <br />
        <input type="checkbox" name="tos" value="1" required /> By clicking on this box, you accept our Terms And Conditions.
        <br />
        <input class="left t10" style="cursor:pointer;" type="submit" name="order" value="Submit Order">
        <br />
      </form>
    </div>
    <!--end .accordion-section-content-->
  </div>
  <!--end .accordion-section-->
  <div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-3">Booking &raquo; Step 3 <i class="right fi-plus"></i></a>
    <div id="accordion-3" class="accordion-section-content">
      <div id="confirm_result"></div>
    </div>
    <!--end .accordion-section-content-->
  </div>
  <!--end .accordion-section-->
</div>
<!--end .accordion-->
<script>
  </script>

如果我对数组逻辑的描述是正确的,下面是为您编写的代码:

Function arrarResorter( array $info ) {
    $return = [];
    foreach ($info['adults'] as $adult => $room ) {
        $return[$room]['adults'][] = [
            'adult_name' => $info['adult_name'][$adult],
            'adult_age' => $info['adult_age'][$adult],
            'adult_gender' => $info['adult_gender'][$adult]
        ];
    }
    foreach ($info['children'] as $children => $room ) {
        $return[$room]['children'][] = [
            'children_name' => $info['children_name'][$children],
            'children_age' => $info['children_age'][$children],
            'children_gender' => $info['children_gender'][$children]
        ];
    }
    return $return;
}

试试吧