为酒店系统创建嵌套的json

时间:2019-06-19 02:07:38

标签: php json

我想创建一个post附带的嵌套json。 我有3个领域。成人,儿童和婴儿。但是,也有儿童和婴儿区的约会。例如;儿童1岁生日,儿童2岁生日,婴儿1岁生日,婴儿2岁生日 当我尝试使用以下代码时,无法按房间打印子日期。批量写入。如何在下面用JSON之类的php发布信息?

 +---------+----------+-------+
 | Adults  | Children | Baby  | (select box field)
 +--------------------+--------
 | 1       | 0        | 0     |
 +--------------------+--------
 | 2       | 1        | 1     | (also available for children and baby areas)
 +--------------------+-------+
 | 3       | 2        | 2     |
 +---------+----------+-------+

rooms: [
         {
            adults: 2,
            child: [
               child_total: 1
               child_date: [
                   "2017-08-10"
               ],
            ],
            baby: [
               baby_total: 1
               baby_date: [
                   "2017-07-01"
               ],
            ],
         },
         {
            adults: 1,
            child: [
               child_total: 2
               child_date: [
                   "2017-08-08",
                   "2017-08-09"
               ],
            ],
            baby: [
               baby_total: 2
               baby_date: [
                   "2017-06-08",
                   "2017-05-09"
               ],
            ],
         }
      ],

        foreach ($this->input->post('adults') as $key => $value) {

            foreach ($_POST['child_total'] as $value2) {
                $child_date = [];
                foreach ($_POST['child_date'] as $key2 => $value3) {
                    $child_date[] = $value3;
                }
            }

            $rooms[] = array(
                'adults' => $_POST['adults'][$key],
                'child' => array(
                    'child_total' => $_POST['child_total'][$key],
                    'child_date' => $child_date
                )
            );
        }
        print json_encode($rooms, JSON_PRETTY_PRINT);

0 个答案:

没有答案