2013-11-02 29 views
1

我有一個二維數組,其中包含有關父級cateogries(parent=1)的信息。下面是該陣列的print_r()CodeIgniter不斷在數據庫查詢中返回一個空數組

Array 
(
    [0] => Array 
     (
      [id] => 2 
      [title] => parent2 
      [content] => this is an example 
      [avator] => 
      [thumnail] => 
      [parent] => 1 
      [parent_id] => 8 
     ) 

    [1] => Array 
     (
      [id] => 3 
      [title] => THISPARENT 
      [content] => A NOTE 
      [avator] => 
      [thumnail] => 
      [parent] => 1 
      [parent_id] => 12 
     ) 

) 

在這陣我有兩個家長。只有其中一個在數據庫表中有一個孩子,所以當我在一個孩子的數據庫中搜索parent_id應該是其中的一個時,邏輯結果應該是一個包含一個子類別信息的數組,而它返回兩個數組,其中一個是空的。

我通過使用下面的loop使用上述父cateogries' IDS搜索DB:

for($i=0; $i<count($cats); $i++) 
     { 
      $this->db->where("parent_id", $cats[$i]['id']); 
      $res = $this->db->get("category"); 
      $x[$i] = $res->result_array(); 
     } 

現在,我得到的結果是在這裏:

    Array 
(
    [0] => Array 
     (
     ) 

    [1] => Array 
     (
      [0] => Array 
       (
        [id] => 1 
        [title] => child 
        [content] => childparent it is 
        [avator] => 
        [thumnail] => 
        [parent] => 0 
        [parent_id] => 3 
       ) 

     ) 

) 

雖然應不是一個空的零索引數組,我只希望收到數組,這是我已經作爲第二個索引,第一個空!爲什麼?

在此先感謝

回答

0

你正在接受因爲

$x[$i] = $res->result_array(); 

嘗試零索引的數組將其更改爲

$results = $res->result_array(); 
if (is_array($results) && count($results)>0) { 
    $x[$i] = $results; 
} 

那麼就應該跳過零索引和所有空數組。

+0

謝謝我今晚會檢查一下並報告結果 –

+0

嘿,它工作得很好。謝謝。 –