从HTML表单关联数组构建PHP数组结构

时间:2015-03-03 20:38:29

标签: php html arrays

我想从下面的html表单构建一个php数组结构 我有以下HTML表单

<!-- 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>

到目前为止我的PHP代码是

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

这是有效的方法,但它没有按预期工作(不计算儿童年龄)。 作为预期输出的示例:房间1 = 1成人选择房间2 =有2个成人选择和1个孩子选择谁有8岁

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

        )

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

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

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

        )

)

如果您需要更多信息,请告诉我们。

2 个答案:

答案 0 :(得分:1)

我认为变量$ y是错误的。 请尝试以下代码:

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

<强>更新

/**
 * @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)
        );
    }
}

整数使用:

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

字符串用法:

$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',
  ),
)

您也可以查看php.net手册: http://php.net/manual/en/function.array-push.php

答案 1 :(得分:0)

尝试更改

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

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

你想要所有&#34;孩子&#34;有相同的年龄?如果,如果用户选择房间[1] [孩子] = 3并且房间[1] [年龄] = 5,你是否想要所有3&#34;儿童&#34;数组有&#34;年龄&#34;设为5?