简体   繁体   中英

Build PHP array structure from HTML form associative arrays

I want to build an php array structure from the below html form.
I have the below HTML form

<!-- ROOM 1-->
 <div class="row">
       <div class="col-xs-4">
          <label>ADULTS</label>
          <div  class="selector">
             <select id='adults-1' name="rooms[0][adult]" class="full-width">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
             </select>
          </div>
       </div>
       <div class="col-xs-4">
          <label>Copii</label>
          <div  class="selector">
             <select id='kids-1'  name="rooms[0][child]" class="full-width">
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
             </select>
          </div>
       </div>
    </div>
    <div class="age-of-children no-display">
       <div class="row">
          <div class="col-xs-4 child-age-field">
             <label>Copil 1</label>
             <div class="selector validation-field">
                <select class="full-width" name="rooms[0][age]" id='age-1'>
                   <option value="3">3</option>
                   <option value="4">4</option>
                   <option value="5">5</option>
                   <option value="6">6</option>
                   <option value="7">7</option>
                   <option value="8">8</option>
                   <option value="9">9</option>
                   <option value="10">10</option>
                   <option value="11">11</option>
                </select>
             </div>
          </div>
       </div>
    </div>
    <!-- ROOM 2-->
 <div class="row">
       <div class="col-xs-4">
          <label>ADULTS</label>
          <div  class="selector">
             <select id='adults-1' name="rooms[1][adult]" class="full-width">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
             </select>
          </div>
       </div>
       <div class="col-xs-4">
          <label>Copii</label>
          <div  class="selector">
             <select id='kids-1'  name="rooms[1][child]" class="full-width">
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
             </select>
          </div>
       </div>
    </div>
    <div class="age-of-children no-display">
       <div class="row">
          <div class="col-xs-4 child-age-field">
             <label>Copil 1</label>
             <div class="selector validation-field">
                <select class="full-width" name="rooms[1][age]" id='age-1'>
                   <option value="3">3</option>
                   <option value="4">4</option>
                   <option value="5">5</option>
                   <option value="6">6</option>
                   <option value="7">7</option>
                   <option value="8">8</option>
                   <option value="9">9</option>
                   <option value="10">10</option>
                   <option value="11">11</option>
                </select>
             </div>
          </div>
       </div>
    </div>

And thw PHP code that i have so far is

    $result = array();
if(!empty($_POST['rooms'])) {
    foreach($_POST['rooms'] as $k => $info) {
        for($y = 0; $y < $info['adult']; $y++) {
            $result[$k][]['paxType'] = 'Adult';
        }

        for($y = 0; $y < $info['child']; $y++) {
            $result[$k][]['paxType'] = 'Child';
        }
    }
}

This is working some how but its not working as expected (its not counting the child age). As an example expected output: Room 1 = 1 Adult selected Room 2 = has 2 Adult Selected and 1 Child selected who has 8 years old

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [paxType] => Adult
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [paxType] => Adult
                )

            [1] => Array
                (
                    [paxType] => Adult
                )

            [2] => Array
                (
                    [paxType] => Child
                    [age] => 8
                )

        )

)

If you need any more information please let me know.

I think variable $y is wrong. Please try following code:

$result = array();
if(!empty($_POST['rooms'])) {
    foreach($_POST['rooms'] as $k => $info) {
        for($y = 0; $y < $info['adult']; $y++) {
            $result[$k][]['paxType'] = 'Adult';
        }

        for($z = 0; $z < $info['child']; $z++) {
            $result[$k][]['paxType'] = 'Child';
        }
    }
}

Update:

/**
 * @param array      $array
 * @param int|string $position
 * @param mixed      $insert
 */
function array_insert(&$array, $position, $insert)
{
    if (is_int($position)) {
        array_splice($array, $position, 0, $insert);
    } else {
        $pos   = array_search($position, array_keys($array));
        $array = array_merge(
            array_slice($array, 0, $pos),
            $insert,
            array_slice($array, $pos)
        );
    }
}

Integer usage:

$arr = ["one", "two", "three"];
array_insert(
    $arr,
    1,
    "on-half"
);
// ->
array (
  0 => 'one',
  1 => 'on-half',
  2 => 'two',
  3 => 'three',
)

String Usage:

$arr = [
    "name"  => [
        "type"      => "string",
        "maxlength" => "30",
    ],
    "email" => [
        "type"      => "email",
        "maxlength" => "150",
    ],
];

array_insert(
    $arr,
    "email",
    [
        "phone" => [
            "type"   => "string",
            "format" => "phone",
        ],
    ]
);
// ->
array (
  'name' =>
  array (
    'type' => 'string',
    'maxlength' => '30',
  ),
  'phone' =>
  array (
    'type' => 'string',
    'format' => 'phone',
  ),
  'email' =>
  array (
    'type' => 'email',
    'maxlength' => '150',
  ),
)

Also you can check the php.net manual : http://php.net/manual/en/function.array-push.php

Try changing

for($y = 0; $y < $info['child']; $y++) {
    $result[$k][]['paxType'] = 'Child';
}

to

for($y = 0; $y < $info['child']; $y++) {
    $result[$k][] = array('paxType' => 'Child', 'age' => $info['age']);
}

Did you want all "Child" to have the same age? As in, if the user selects rooms[1][child] = 3 and rooms[1][age] = 5, did you want all 3 "Child" array to have "age" set to 5?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM