//我有这样的数组:
$roms = array (
[1] => 2
[2] => 4
[6] => 2
[7] => 7
)
//我有像下面这样的数据表:
ID room total_room
1 1b/1b 7
2 2b/2b 12
6 3b/ 2b 32
8 3b/3b 7
//查询结果时
$total_rooms = 0 ;
foreach( $query->result() as $row )
{
if ( $row->ID , array_keys($rooms) ) :
$total_rooms[] = array_values( $rooms ); // from array values
else:
$total_rooms[] = $row->total_room; // from table
endif;
}
//我想要那样的结果:
$total_rooms = 2; // array value
$total_rooms = 4; // array value
$total_rooms = 2; // array value
$total_rooms = 7; // not array value : becos no ID in keys
但是这段代码对我不起作用!
有人能帮助我吗?
答案 0 :(得分:0)
你的if语句和以下行有点奇怪,试试这个:
if(array_key_exists($row->ID, $rooms)) :
$total_rooms[] = $rooms[$row->ID]; // from array
else:
$total_rooms[] = $row->total_room; // from table
endif;
注意:您每次都要为$total_rooms
分配一个新的数组键,您确定不要只是想让它成为常规的吗?变量?参见:
$total_rooms = $rooms[$row->ID]; // from array