2016-07-24 72 views
0

我有我的data arrayusersbooks 這就是我如何創建此數組在我的控制器笨:鑑於解析陣列

public function getuserhistory($id){ 
      $query= $this->db->get_where('book_assign', array(
       'user_id'  => $id, 
      )); 
      return $query->result(); 
     } 
     public function booksrecord($id){ 
      $books= $this->db->get_where('books',array('id' => $id)); 
      return $books->result(); 
     } 
public function history($id){ 
     $results['user']= $this->usermodel->getuserhistory($id); 
     foreach ($results as $value) { 
      foreach ($value as $subvalue) { 
       $results[]['books']= $this->usermodel->booksrecord($subvalue->id); 
      } 
     } 
     $data['data'] = $results; 
$this->load->view('history', $data); 
} 

下面是我得到

Array 
(
    [user] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 1 
        [user_id] => 5 
        [book_id] => 1 
        [date_issue] => 2016-07-24 00:00:00 
        [date_return] => 2016-07-25 00:00:00 
       ) 

      [1] => stdClass Object 
       (
        [id] => 2 
        [user_id] => 5 
        [book_id] => 2 
        [date_issue] => 2016-07-24 00:00:00 
        [date_return] => 2016-07-25 00:00:00 
       ) 

     ) 

    [0] => Array 
     (
      [books] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => 1 
          [title] => PHP Made Easy 
          [author] => Dietel & Dietel 
          [serial_no] => 232323 
          [qty] => 9 
          [row_no] => 1 
          [col_no] => 2 
          [status] => 1 
          [category_id] => 1 
          [description] => This is a book about php 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [books] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => 2 
          [title] => C++ 
          [author] => Dietel & Dietel 
          [serial_no] => 232323 
          [qty] => 9 
          [row_no] => 1 
          [col_no] => 2 
          [status] => 1 
          [category_id] => 1 
          [description] => This is a book about c++ 
         ) 

       ) 

     ) 

) 
數組2個數組索引

這個數組有一個用戶索引中的特定用戶和書籍索引中分配給該用戶的書籍,我必須以可在表格中生成一行的方式解析此數組,這樣我可以顯示分配給用戶的每本書在一個單獨的行中。請helpe我來分析這個數組 這是我打印

<tr> 
    <th>User id </th> 
    <th>Book Title </th> 
    <th>Date Issued</th> 
    <th> Date Return</th> 
    <th> Action</th> 
</tr> 

回答

0

你父級陣列是因爲第一個元素,它的內容不一致是一個闖入者的格式 - 只有一個是用戶數據,而其他元素每本書的數據。我們可以假設有時會有更多的書籍被返回。爲了保持這種方式的一致性,我會將這些數組分爲兩個數組,第一個存儲用戶數據,第二個存儲數組。現在

$user = array_shift($parent); 
$books = $parent;// convinient naming for later use 

,可以循環用戶和使用書ID

if (count($user)) 
{ 
    foreach($user as $obj) 
    { 
     $rows = "<tr><td>$obj->user->id</td>"; 
     foreach($books as $book) 
     { 
      if($book->id == $obj->book_id) 
      { 
       $rows .= "<td>$book->title</td>"; 
      } 
      break; 
     } 
     $rows .= "<td>$obj->date_issued</td>"; 
     $rows .= "<td>$obj->date_returned</td>"; 
     $rows .= "<td>Action</td></tr>"; 
    } 
    echo $rows; 
} 

勾選此作品獻給合適的書。 雖然這個(sh | c)可以工作,但請參閱從數據庫返回更少的數據並始終嘗試使用DRY方法。在這裏你可以返回許多對象(數組中的用戶鍵),只需要輸入不同的book id即可。這是非常昂貴的方法,你可以檢查如何使用數據庫中的concat書籍ID或類似的功能。