PHP - foreach with map

Introduction

The foreach loops are useful with maps.

The order in which PHP iterates the array will be the same order that you used to insert the contents in the array.

Demo

<?php 
        $books = [ /*  w  w w.j  a  v a  2  s. c  o m*/
            [ 
                'title' => 'Java Tutorial', 
                'author' => 'Jason', 
                'available' => true, 
                'pages' => 336, 
                'isbn' => 000001 
            ], 
            [ 
                'title' => '2018', 
                'author' => 'C', 
                'available' => true, 
                'pages' => 267, 
                'isbn' => 0002 
            ], 
            [ 
                'title' => 'Javascript', 
                'author' => 'Gason', 
                'available' => false, 
                'pages' => 457, 
                'isbn' => 0003 
            ], 
        ]; 
        foreach ($books as $book){
            echo $book['title'] . '\n';
            echo $book['author'] . '\n';
            if (!$book['available']){
              echo 'Not available' . '\n';
            }
        }
?>

Related Topic