但我在执行以下代码时收到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();
}
答案 0 :(得分:1)
改变这个 $ arrVals ['rt_name'] = $ this-&gt; reservations_model-&gt; pop_room_type(); / *这是第一个数组: 至 $ arrVals = $ this-&gt; reservations_model-&gt; pop_room_type(); / *这是第一个数组:
改变这个 $ arrKeys ['rt_default_price'] = $ this-&gt; reservations_model-&gt; pop_room_price(); / *这是第二个数组: 至 $ arrKeys = $ this-&gt; reservations_model-&gt; pop_room_price(); / *这是第二个数组:
答案 1 :(得分: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]));
}
请注意,这取决于索引是否相同;如果情况并非总是如此,则应添加检查以确保索引存在。
reset函数返回数组中的第一个元素。