array_search结果没有正确

时间:2016-10-19 08:48:50

标签: mysql arrays codeigniter-3

这里我有两张桌子,即房间和student_hostel。 房间看起来像这样

    id  rm_number  capacity     bed_no             class  hostel    is_vaccant
    40    1           5     1A,1B,1C,1D,1E          27      7          1
    41    2           4     2A,2B,2C,2D             28      7          1
    42    3           3     3A,3B,3C                29      10         1
    43    4           4     4A,4B,4C,4D             30      10         1
    44    5           6     5A,5B,5C,5D,5E,5F       27      7          1
    45    6           7     6A,6B,6C,6D,6E,6F,6G    29      10         1

student_hostel看起来像这样

    id    first_name       stud_id    hostel   class    room    bed     status
    175     siraj         WPGH00175     7      28        41      2A     P
    176     nesru         WPGH00176     7      28        41      2B     P
    180     dsf           WPGH00180     7      27        40      1A     G

以下是我试图获取所有占用房间的代码

控制器看起来像这样

public function beds_occupied($rm)
{
    $data['bed'] = $this->admin_model->ajax_bed($rm)->row();
    $data['beds'] = explode(',',$data['bed']->bed_no);
    $data['bed_no'] = $this->admin_model->get_bed_no($rm)->result();
    foreach ($data['bed_no'] as $row) 
    {

        if (($key = array_search($row->bed, $data['beds'])) !== false) 
        {


            unset($data['beds'][$key]);

            $data['beds'] = array_values($data['beds']);


        }
    }
    return $data['beds'];
}   

模型看起来像这样

public function ajax_bed($val=Null)
{
    if(isset($val))
    {
        $this->db->where('id',$val);    
    }
    return $this->db->get('rooms'); 
}

public function get_bed_no($id)
{
    return $this->db->get_where('student_hostel',array('room'=>$id));
}
我得到的输出是所有接种床而不是占用, 我得到的结果是这样的。

SI no  Rooms    Class          Hostel         Occupied  
 1      1      periyar1       periyar        1B,1C,1D,1E    
 2      2      periyar2       periyar        2C,2D  
 3      3      pamba1         pamba          3A,3B,3C   
 4      4      pamba 2        pamba          4A,4B,4C,4D    
 5      5      periyar1       periyar        5A,5B,5C,5D,5E,5F      
 6      6      pamba1         pamba          6A,6B,6C,6D,6E,6F,6G

但我希望得到这样的

 SI no  Rooms    Class         Hostel         Occupied  
 1      1      periyar1       periyar        1A 
 2      2      periyar2       periyar        2A,2B  
 3      3      pamba1         pamba          
 4      4      pamba 2        pamba             
 5      5      periyar1       periyar               
 6      6      pamba1         pamba          

1 个答案:

答案 0 :(得分:1)

您正在取消存在的变量。你想保留在床位时存在的变量。试试这个:

public function beds_occupied($rm)
{
    $occupied = array();
    $data['bed'] = $this->admin_model->ajax_bed($rm)->row();
    $data['beds'] = explode(',',$data['bed']->bed_no);
    $data['bed_no'] = $this->admin_model->get_bed_no($rm)->result();

    foreach ($data['bed_no'] as $row) 
    {
        $key = array_search($row->bed, $data['beds'];
        if ($key) 
        {
            $occupied[] = $data['beds'][$key]);
        }
    }
    return $occupied;
}