2016-07-25 9 views
-1

Ich habe ein Array im Bibliotheksverwaltungssystem, das einen Benutzer auf dem ersten Index hat, Bücher, die diesem Benutzer auf dem nächsten Index zugewiesen sind, und dann Details über Bücher, die so zugeordnet sind.PHP: Mehrdimensionales Array durchqueren

das ist, was ich in Tabellenform

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 
         ) 

       ) 

     ) 

) 

dies zeigen, müssen, wie ich versuche, wie diese

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

den Fehler i

erhalten zu analysieren

Bewertung: Warnung

Nachricht: Illegal Offset-Typ

Dateiname: views/history.php

Zeilennummer: 4

Bitte helfen Sie mir dieses Array

+0

was meinst du mit "Parsing"? Was ist das erwartete Ergebnis? meinst du "traversing" stattdessen? – fantaghirocco

+0

bitte posten endgültige Ausgabe möchten Sie –

+0

Ich meine, ich muss diese Ergebnisse in HTML-Tabelle drucken – Sikander

Antwort

0

dieses Array-Objekt zu analysieren, versuchen um ein Objekt als Array zu erhalten, indem man so vorgeht.

Dies wird Ihr Array-Objekt in Array-Format konvertieren. oder sonst können Sie unter Funktion

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; 
} 

Dies ist nicht eine tatsächliche direkte Antwort auf Ihre Frage, aber ich Sie darauf hindeutet, dass Sie auch auf diese Weise versuchen kann, Wenn die Antwort Zeichen als Antwort geholfen wird :).

0

Ich würde Sie dies vorschlagen,

  1. Stopp diese Art von mehrdimensionalen Arrays Looping. Diese Array-Bildung ist falsch, wenn Sie erwarten, dass die Ausgabe in der Post angezeigt wird.

  2. Versuchen zusätzliche unnötige Verschachtelung

     [0] => Array (
          [0] => stdClass Object 
           (
            [id] => 1 
            [title] => PHP Made Easy 
    
  3. Teilen Sie Array zu entfernen und sie in andere Variable für beispielsweise sparen

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

    $ books = $ array [Bücher];

    $ bookdata = $ array [Buchdaten];

  4. Hier können Sie fortfahren und Daten zu Ihrem leeren Array hinzufügen, das Schlüssel/pro Datensatz verwaltet. Dieser Schlüssel hilft Ihnen, ihn in einer Tabelle zu loopen ...

0

Je nach Array-Struktur ist es ein Array-Objekt.

Sie müssen den Namen des Call-Array-Schlüssels verwenden, um Array-Objekte innerhalb Ihrer Hauptschleife zu durchlaufen. So können Sie es wie unten erwähnt tun.

//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; 
    } 

} 

Jetzt können Sie dies verwenden, um HTML-Tabelle zu füllen.

0

Da Sie nur einen einzigen Benutzer pro Array haben, können Sie das Array einfach durchlaufen; Verwenden Sie conditional Logic, um den aktuellen Schlüssel zu bestimmen (wie Bücher, Benutzer) ... und von dort können Sie einfach eine HTML-Tabelle erstellen, die Ihre benötigte Ausgabe enthält. Ein Test könnte in Auftrag und you can find the test here sein.

<?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;