-1

但执行以下代码时收到 2 个错误:

消息:未定义索引:rt_default_price 文件名:controllers/reservations.php 行号:24

消息:非法偏移类型文件名:controllers/reservations.php 行号:24

第 24 行是...

$combined[] = array($key => $arrVals[$i]);

控制器(reservations.php)

$arrVals['rt_name'] = $this->reservations_model->pop_room_type(); /* This is the 1st Array:

Array
(
[rt_name] => Array
    (
        [0] => Array
            (
                [rt_name] => Business
            )

        [1] => Array
            (
                [rt_name] => Econ
            )

        [2] => Array
            (
                [rt_name] => Luxury
            )

        [3] => Array
            (
                [rt_name] => VIP
            )

    )

)
*/

$arrKeys['rt_default_price'] = $this->reservations_model->pop_room_price(); /* This is the 2nd Array:


Array
(
[rt_default_price] => Array
    (
        [0] => Array
            (
                [rt_default_price] => 50000
            )

        [1] => Array
            (
                [rt_default_price] => 25000
            )

        [2] => Array
            (
                [rt_default_price] => 75000
            )

        [3] => Array
            (
                [rt_default_price] => 100000
            )

    )

)
*/



$combined=array();
foreach ($arrKeys as $i => $key) {
$combined[] = array($key => $arrVals[$i]); // Line 24
}

/*echo "<pre>";
print_r($combined);
echo "</pre>";

Array
(
[0] => Array
    (
    )

)
*/

$this->load->view('/main/new_reservation', $combined);

查看 (main/new_reservation.php)

<?php
echo form_dropdown('room_type', $combined);
?>    

模型(reservation_model.php)

function pop_room_type() {
    $this->db->select('rt_name');
    $query=$this->db->get('room_type');
    return $query->result_array();
}

function pop_room_price() {
    $this->db->select('rt_default_price');
    $query=$this->db->get('room_type');
    return $query->result_array();
}
4

2 回答 2

1

更改此 $arrVals['rt_name'] = $this->reservations_model->pop_room_type(); /* 这是第一个数组:to $arrVals = $this->reservations_model->pop_room_type(); /* 这是第一个数组:

更改此 $arrKeys['rt_default_price'] = $this->reservations_model->pop_room_price(); /* 这是第二个数组:to $arrKeys = $this->reservations_model->pop_room_price(); /* 这是第二个数组:

于 2013-07-05T06:08:28.870 回答
1

我想我理解你想要达到的目标,但如果我错了,请纠正我。

$combined=array('rt_name'=>array());
foreach ($arrKeys['rt_default_price'] as $i => $defaultPrice) {
  $combined['rt_name'][$i]=array(reset($defaultPrice)=>reset($arrVals['rt_name'][$i]));
}

请注意,这确实取决于索引是否相同;如果情况并非总是如此,则应添加检查以确保索引存在。

重置函数返回数组中的第一个元素。

于 2013-07-05T06:26:21.190 回答