2016-07-25 169 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;