2016-07-25 166 views
-1

我有一個圖書館管理系統中的數組,在第一個索引上有一個用戶,在下一個索引上分配給該用戶的書籍,然後是有關像這樣分配的書籍的詳細信息。PHP:穿越多維陣列

這就是我需要以表格形式顯示

enter image description here

Array 
(
    [user] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 5 
        [name] => 
        [email] => [email protected] 
        [password] => test 
        [role] => 
        [status] => 1 
       ) 

     ) 

    [books] => 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 
       ) 

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

     ) 

    [bookdata] => Array 
     (
      [0] => 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 
       (
        [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] => 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 
         ) 

       ) 

     ) 

) 

這是我嘗試解析這樣

foreach ($data as $key=> $value) { 
     echo $key[$value]->id; 
    } 

錯誤我得到

嚴重性:警告

消息:非法偏移類型

文件名:觀點/ history.php

行號:4

請幫我解析這個陣列

+0

你是什麼意思的「解析」?預期的結果是什麼?你的意思是「遍歷」而不是? – fantaghirocco

+0

請發佈你想要的最終輸出 –

+0

我的意思是我需要在html表中打印這些結果 – Sikander

回答

0

這個數組對象,嘗試像這樣使用數組來獲得對象。

$data_new = (array) $data; 

這會將您的數組對象轉換爲數組格式。之後您可以將其傳遞給foreach。 否則,你可以使用以下功能

function object_to_array($obj) { 
    $arr = is_object($obj) ? get_object_vars($obj) : $obj; 
    foreach ($arr as $key => $val) { 
      $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val; 
      $arr[$key] = $val; 
    } 
    return $arr; 
} 

這不是你的問題的實際直接的答案,但我建議你,你可以用這種方式嘗試過,如果答案是幫助標記爲答案:)。

0

我建議你,

  1. 停止循環這種類型的多維數組。如果您希望在後期顯示輸出,則此陣列形成錯誤。

  2. 嘗試刪除多餘的不必要的嵌套

     [0] => Array (
          [0] => stdClass Object 
           (
            [id] => 1 
            [title] => PHP Made Easy 
    
  3. 您陣列鴻溝,並將其保存在例如不同的變量

    $ userInfo = $ array [user] [0];

    $ books = $ array [books];

    $ bookdata = $ array [bookdata];

  4. 在此之後,您可以循環並開始將數據添加到維持鍵/每條記錄的空陣列。這個鍵將幫助你將它循環在一個表中...

0

根據你的數組結構,它是一個數組對象。

您必須使用調用數組鍵名來遍歷主循環中的數組對象。所以你可以像下面提到的那樣做。

//main loop 
foreach ($data as $key=> $value) { 

    //get user info 
    echo $value['user'][0]->id; 

    //get books info 
    for($i=0; $i < count($value['books']); $i++) { 
     echo $value['books'][$i]->id; 
    } 

    //get further books info 
    foreach($value['bookdata'] as $books) { 
     echo $books->author; 
    } 

} 

現在您可以使用它來填充HTML表格。

0

由於每個數組只有一個用戶,因此您可以循環訪問該數組;使用conditional Logic來決定當前密鑰(如書籍,用戶)......並從那裏,你可以建立一個包含你所需輸出的HTML表格。一個測試可能在Order和you can find the test here

<?php 

    $tblOutput = "<div class='books-wrapper col-md-12'>"        . PHP_EOL; 
    if(!empty($arrBkLib)){ 

     foreach($arrBkLib as $key=>$data){ 
      if($key == 'user' && !empty($data[0])){ 
       $userName = isset($data[0]->name) ? $data[0]->name : "N/A"; 

       $tblOutput .= "<div class='user-data-pane col-md-6 pull-left'>"  . PHP_EOL; 
       $tblOutput .= "<span class='btn btn-custom'>{$userName}</span>"   . PHP_EOL; 
       $tblOutput .= "</div>"             . PHP_EOL; 

       $tblOutput .= "<div class='action-pane col-md-6 pull-right'>"   . PHP_EOL; 
       $tblOutput .= "<a class='btn btn-custom'>Ban User</a>"     . PHP_EOL; 
       $tblOutput .= "</div>"             . PHP_EOL; 


       $tblOutput .= "<table class='tbl-books-data'>"       . PHP_EOL; 

       $tblOutput .= "<tr class='tbl-books-data-head-row'>"     . PHP_EOL; 
       $tblOutput .= "<th class='tbl-books-data-head-cell'>Book</th>"   . PHP_EOL; 
       $tblOutput .= "<th class='tbl-books-data-head-cell'>Issue Date</th>" . PHP_EOL; 
       $tblOutput .= "<th class='tbl-books-data-head-cell'>Return Date</th>" . PHP_EOL; 
       $tblOutput .= "<th class='tbl-books-data-head-cell'>Fine</th>"   . PHP_EOL; 
       $tblOutput .= "<th class='tbl-books-data-head-cell'>Status</th>"  . PHP_EOL; 
       $tblOutput .= "</tr>"             . PHP_EOL; 

      }else if($key == 'books' && !empty($data)){ 
       foreach($data as $iKey=>$books) { 
        $issueDate = isset($books->date_issue) ? date("d/m/Y", strtotime($books->date_issue)) : "N/A"; 
        $returnDate = isset($books->date_return) ? date("d/m/Y", strtotime($books->date_return)) : "N/A"; 
        $bookName = isset($arrBkLib['bookdata'][$iKey]->title) ? $arrBkLib['bookdata'][$iKey]->title : "N/A"; 
        $status  = isset($arrBkLib['bookdata'][$iKey]->status) ? $arrBkLib['bookdata'][$iKey]->status : "N/A"; 
        $fine  = "N/A"; // USE YOUR LOGIC HERE TO DETERMINE THIS VALUE... 

        $tblOutput .= "<tr class='tbl-books-data-row'>"      . PHP_EOL; 
        $tblOutput .= "<th class='tbl-books-data-cell'>{$bookName}</th>" . PHP_EOL; 
        $tblOutput .= "<th class='tbl-books-data-cell'>{$issueDate}</th>" . PHP_EOL; 
        $tblOutput .= "<th class='tbl-books-data-cell'>{$returnDate}</th>" . PHP_EOL; 
        $tblOutput .= "<th class='tbl-books-data-cell'>{$fine}</th>"  . PHP_EOL; 
        $tblOutput .= "<th class='tbl-books-data-cell'>{$status}</th>"  . PHP_EOL; 
        $tblOutput .= "</tr>"            . PHP_EOL; 
       } 
      } 
     } 
     $tblOutput .= "</table>" . PHP_EOL; 
     $tblOutput .= "</div>" . PHP_EOL; 
    } 

echo $tblOutput;