Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am trying to create a reservation form that will allow my users to input data for a room. In the room the user can add multiple adults or children and the name and gender of each.

So, my form gives me this array:

Array
(
    [room] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
        )

    [adults] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 2
            [3] => 1
            [4] => 2
        )

    [adult_name] => Array
        (
            [0] => Adult 1 Name
            [1] => Adult 2 Name
            [2] => Adult 3 Name
            [3] => Adult 4 Name
            [4] => Adult 5 Name
        )

    [adult_age] => Array
        (
            [0] => 05-08-2009
            [1] => 03-07-1974
            [2] => 04-03-2006
            [3] => 05-05-1989
            [4] => 04-05-1969
        )

    [adult_gender] => Array
        (
            [0] => Female
            [1] => Male
            [2] => Female
            [3] => Male
            [4] => Female
        )

    [childs] => Array
        (
            [0] => 1
            [1] => 1
        )

    [child_name] => Array
        (
            [0] => Child 1 Name
            [1] => Child 2 Name
        )

    [child_age] => Array
        (
            [0] => 02-04-2006
            [1] => 16-03-2008
        )

    [child_gender] => Array
        (
            [0] => Female
            [1] => Male
        )

)

However, I need to rebuild it to return an array in this form:

Array
(
    [room] => Array(

        [0] => array(

            [adults] => Array(

                    [0] => array(

                        [adult_name] = Adult 1 Name,
                        [adult_age] = 03-07-1974,
                        [adult_gender] = Female,

                    )

                    [1] => array(

                        [adult_name] = Adult 2 Name,
                        [adult_age] = 03-07-1974,
                        [adult_gender] = Female,

                    )
            [childs] => Array(

                    [0] => array(

                        [child_name] = Adult 1 Name,
                        [child_age] = 03-07-2000,
                        [child_gender] = Female,

                    )

                )
            ),

        [1] => array(

            [adults] => Array(

                    [0] => array(

                        [adult_name] = Adult 1 Name,
                        [adult_age] = 03-07-1994,
                        [adult_gender] = Male,

                    )

            [childs] => Array

                    (

                        [0] => array(

                               [child_name] = Child 1 Name,
                               [child_age] = 03-07-2002,
                               [child_gender] = Female,

                        )


                    )

            [childs] => Array

                    (

                        [1] => array(

                            [child_name] = Child 2 Name,
                            [child_age] = 03-07-2003,
                            [child_gender] = mALE,

                        )


                    )
            )
        )

    )

)

Is this possible? Here is the code I have so far:

$(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="[email protected]" 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>

share|improve this question
    
Is Array room[0] always going to correspond with array adults[0] and so on? – The Angry Dev Sep 5 at 0:23
    
Thank you for your reply, i m fraid that is not Like this, in rooms[o] can be two adults and 1 children... So the keys are not same... Or I m wrong? Can you give me a foreach example? – George Sep 5 at 0:26
    
foreach might work here but you might also consider json. If the arrays don't have corresponding values (ie 0=>0 down the line) then you would need an identifier to match the data together. Where is the source array being populated from? Is it a database? You might also be able to write a more constructive query if that's the case. – The Angry Dev Sep 5 at 0:32
2  
Could you explain the logic behind the conversion? – Berriel Sep 5 at 0:51
1  
Could you show the form html or an example? That might help too. There are ways to bind arrays to form objects before posting so you can parse the data out more easily. – The Angry Dev Sep 5 at 0:55

1 Answer 1

If i desciphered your array logic right, this is the code for you:

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;
}

Give it a try.

share|improve this answer
    
Thank you for your code, you are almost there but still not what I mean. I will prepare a life demo with my form to show you what I mean. – George Sep 5 at 10:24
    
Voila, i have prepared a code snipped, please check to understand the functionality, run the form so you in step 3 the script return array. – George Sep 5 at 17:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.