PHP - Looping Through Multidimensional Arrays

Introduction

The following example uses two nested foreach loops to loop through the $myBooks array.

Demo

<?php
        $myBooks = array(
          array(/*from  w  w  w  . ja  v a 2s .co m*/
           "title"=>"Java",
           "author"=>"John A",
           "pubYear"=> 2018
       ),
          array(
           "title"=>"Database",
           "author"=>"Franz B",
           "pubYear"=> 2017
       ),
          array(
           "title"=>"Json",
           "author"=>"Oracle",
           "pubYear"=> 2018
       ),
          array(
           "title"=>"MySQL",
           "author"=>"Charles D",
             "pubYear"=> 1859
         ),
        );

          $bookNum = 0;

          foreach ($myBooks as $book) {

            $bookNum++;
            echo"Book #$bookNum:\n";
            foreach ($book as $key => $value) {
              echo"$key : $value \n";
            }
          }

?>

Result

Related Topic