Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active April 29, 2024 23:28
Show Gist options
  • Save JeffreyWay/40fcd41c03de5805aedc62ac847094df to your computer and use it in GitHub Desktop.
Save JeffreyWay/40fcd41c03de5805aedc62ac847094df to your computer and use it in GitHub Desktop.
PHP For Beginners, Episode 7 - Associative Arrays https://laracasts.com/series/php-for-beginners-2023-edition/episodes/7
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<?php
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'releaseYear' => 1968,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'releaseYear' => 2021,
'purchaseUrl' => 'http://example.com'
]
];
?>
<ul>
<?php foreach ($books as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name'] ?> (<?= $book['releaseYear'] ?>)
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<?php
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'purchaseUrl' => 'http://example.com'
]
];
?>
<ul>
<?php foreach ($books as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
@letmelivejba
Copy link

<title>Arrays</title> 'All the Light We Cannot See', 'published' => '2005', 'author' => 'Anthony Doerr', 'wiki' => 'https://en.wikipedia.org/wiki/All_the_Light_We_Cannot_See' ], [ 'name' => 'Wolf Totem', 'published' => '2004', 'author' => 'Jiang Rong', 'wiki' => 'https://en.wikipedia.org/wiki/Wolf_Totem' ], [ 'name' => 'The Fault in Our Stars', 'published' => '2012', 'author' => 'John Green', 'wiki' => 'https://en.wikipedia.org/wiki/The_Fault_in_Our_Stars' ], [ 'name' => 'Uncle Styopa', 'published' => '1936', 'author' => 'Sergel Stypa', 'wiki' => 'https://en.wikipedia.org/wiki/Uncle_Styopa' ], [ 'name' => 'Pride and Prejudice', 'published' => '1813', 'author' => 'Jane Austen', 'wiki' => 'https://en.wikipedia.org/wiki/Pride_and_Prejudice' ],
    ];

?>
<ul>
    <?php foreach ($books as $book) : ?>
        <li>
            <a href="<?= $book['wiki']; ?>">
                 <?= $book['name']; ?>
            </a>
    </li>
    <?php endforeach; ?>
</ul>

@AdolpheNkoranyiTresor
Copy link

AdolpheNkoranyiTresor commented May 30, 2023

<title>Learning PHP</title> <style> body{ display: grid; place-items: center; font-family: arial; } </style> // In PHP, the correct syntax for array keys is the => character (equals sign followed by greater-than sign). 'title' => 'Lion and the Mouse', 'publishedYear' => 2009, 'author' => 'Jerry Pinkney', 'purchaseUrl' => 'https://en.wikipedia.org/wiki/The_Lion_%26_the_Mouse' ], [ 'title' => 'Are You My Mother?', 'publishedYear' => 1960, 'author' => 'P. D. Eastman', 'purchaseUrl' => 'https://www.goodreads.com/book/show/197084.Are_You_My_Mother_' ] ]; ?> <ul> <?php foreach ($allBooks as $allBook) : ?> <li> <a href="<?= $allBook['purchaseUrl'] ?>"> <?= $allBook['title']; ?> </a> <?= ' ' . 'was published in' . ' ' . $allBook['publishedYear'] . ' ' . 'and written by' . ' ' . $allBook['author'] . '.'; ?> </li> <?php endforeach; ?> </ul> </div>

@ArsalanOmar
Copy link

'Prison Break', 'release Date' => '15 Sep 2000', 'type' => 'Action', 'status' => 'Old' ], [ 'name' => 'GOT', 'release Date' => '15 Jan 2010', 'type' => 'Action/Thriller, 'status' => 'Latest' ], [ 'name' => 'Breaking Bed', 'release Date' => '15 Aug 2022', 'type' => 'Action/Crime', 'status' => 'Latest' ], ]; ?>
<ul>
    <?php foreach ($movies as $movie): ?>
        <?php if ($movie['status']=='Latest') : ?>
    <li>
       <?= ($movie['name'])  ?>
       <br>
        <?= 'Type:' . ($movie['type'])  ?>
    </li>
    <?php endif ?>
    <?php endforeach ?>
</ul>

@ShyiesPH
Copy link

ShyiesPH commented Jun 8, 2023

`

    <?php
        $books = [
        [
            'name'=> 'The Selection',
            'author'=> 'Alice Gabriel',
            'url'=> 'http:://example.com',
            'year'=> '2013'
        ],
        [
            'name'=> 'The Selection 2',
            'author'=> 'Alice Gabriel',
            'url'=> 'http:://example.com',
            'year'=> '2015'
        ]
        ];
    ?>
<ul>
    <?php foreach ($books as $book) : ?>
        <!-- : is to be replaced by this {}-->
        <li><a href="<?= $book['url'] ?>">
            Title:<?= $book['name'] ?><br>Author:<?= $book['author'] ?><br>Year:<?= $book['year'] ?></a>
        </li>
    <?php endforeach;?>
</ul>

@fauzisofyan27
Copy link

`

<title>Demo Php</title> 'The Power of Self Awareness', 'author' => 'Carlina Patuwo', 'releaseYear' => '2015', 'purchaseUrl' => 'http://example.com' ], [ 'name' => 'You Do You', 'author' => 'Fellexandro Ruby', 'releaseYear' => '2015', 'purchaseUrl' => 'http://example.com' ], ]; ?>
     <ul>
         <?php foreach ($books as $book) : ?>
         <li>
             <a href="<?= $book ['purchaseUrl'] ?>">
                 <?= $book['name'] ?> (<?= $book ['releaseYear'] ?>)
             </a>
         </li>
         <?php endforeach; ?>
     </ul>
`

@michaelaree
Copy link

michaelaree commented Jun 14, 2023

<title>Book List</title> 'Pride and Prejudice', 'author' => 'by Jane Austen', 'releaseYear' => 1813, 'purchaseUrl' => 'https://en.wikipedia.org/wiki/Pride_and_Prejudice' ], [ 'name' => 'To Kill a Mockingbird', 'author' => 'by Harper Lee', 'releaseYear' => 1960, 'purchaseUrl' => 'https://en.wikipedia.org/wiki/To_Kill_a_Mockingbird' ], [ 'name' => 'The Catcher in the Rye', 'author' => 'by J.D. Salinger', 'releaseYear' => 1951, 'purchaseUrl' => 'https://en.wikipedia.org/wiki/The_Catcher_in_the_Rye' ] ]; ?>
<h1>Book List</h1>
<ul>
    <?php foreach ($books as $book): ?>
        <li>
            <a href="<?php echo $book['purchaseUrl']; ?>">
                <?php echo $book['name']; ?>
            </a>
            (<?php echo $book['releaseYear']; ?>)
        </li>
    <?php endforeach; ?>
</ul>

@faithnapeto
Copy link

<title>Document</title> 'The smart woman money', 'author'=> 'Arese Ugwe', 'Url'=> 'eJzj4tVP1zc0TC42TSrKqow3YPQSLclIVSjOTSwqUSjPz03MU8jNz0utBADuMw0C',
],
[
    'name'=> 'The heart of leadership',
    'author'=> 'Joshua freedman',
    'Url'=> 'https://www.google.co.ke/books/edition/The_Heart_of_Leadership/KSsRO4GFZykC?hl=en&gbpv=0',

],

];
?>

        <li>
            <a href="<?= $books['Url'] ?>">
            <?= $books['name'] ?>
    </a>
    </li>
    <?php endforeach; ?>
    </ul>
    

@jcravelo
Copy link

        <?php foreach($books as $book) : ?>

        <li>    
            <a href="<?php echo $book['purchaseUrl'] ?>">
                <?php echo $book['name'].' ('.$book['year'].')';?>
            </a>
        </li>

        <?php endforeach; ?>

@Srdjan0404
Copy link

Srdjan0404 commented Jul 4, 2023

<title>Demo</title>
<style>
    body {
        display: grid;
        place-items: center;
        height: 100vh;
        margin: 0;
        font-family: sens-serif;
    }
</style>
<h1>Recommended books</h1>

<?php
    $books = [
        [
            'name' => 'Harry Potter',
            'author' => 'J. K. Rowling',
            'releaseYear' => '1997',
            'purchaseUrl' => 'http://example.com'
            
        ],
        [
            'name' => 'The Wealth of Nations ',
            'author' =>'Adam Smith',
            'releaseYear' => '1776',
            'purchaseUrl' => 'http://example.com'
            
        ],
        [
            'name' => ' Between the World and Me',
            'author' =>'Siddhartha Mukherjee',
            'releaseYear' => '2011',
            'purchaseUrl' => 'http://example.com'
            
        ],
    ];
?>

<ul>
    <?php foreach($books as $book) : ?>
        <li>
            <a href="<?= $book['purchaseUrl'] ?>"><?= "{$book['name']} ({$book['releaseYear']})"; ?></a>
        </li>
    <?php endforeach; ?>
</ul>

@mahafuzurrahmanhridoy
Copy link

<title>Laracast</title>
<?php

$books = [

    [
        'name' => 'Book 1',
        'author' => 'Robert',
        'releaseYear' => 2017,
        'purchaseUrl' => 'https://book1.com'
    ],

    [
        'name' => 'Book 2',
        'author' => 'Robertson',
        'releaseYear' => 2020,
        'purchaseUrl' => 'https://book2.com'
    ]
];
?>

<ul>
    <?php
    foreach ($books as $book) { ?>

        <li>
            <a href="<?= $book['purchaseUrl'] ?>">
                <?= $book['name']; ?>
            </a>
        </li>

        <li>
            <?= $book['releaseYear']; ?>
        </li>
    <?php } ?>
</ul>

@UtkirbekYuldoshev96
Copy link

<title>PHP || Functions</title>
  <ul>
        <?php foreach($books as $book):?>
              <li>
                    <a href="<?=$book['url']?>">
                          <?=$book['name']?>
                    </a>
              </li>
        <?php endforeach;?>
  </ul>

@UtkirbekYuldoshev96
Copy link

'The Selection', 'author'=> 'Alice Gabriel', 'url'=> 'http:://example.com', 'year'=> '2013' ], [ 'name'=> 'The Selection 2', 'author'=> 'Alice Gabriel 2', 'url'=> 'http:://example.com', 'year'=> '2015' ], [ 'name'=> 'The Selection 3', 'author'=> 'Alice Gabriel', 'url'=> 'http:://example.com', 'year'=> '2015' ], [ 'name'=> 'The Selection 4', 'author'=> 'Gabriel', 'url'=> 'http:://example.com', 'year'=> '2015' ], [ 'name'=> 'The Selection 5', 'author'=> 'Alice ', 'url'=> 'http:://example.com', 'year'=> '2015' ] ]; ?>

@UtkirbekYuldoshev96
Copy link

*{
padding: 0;
margin: 0;
box-sizing: border-box;
}

ul li {
padding: 8px;
margin: 0 auto;
}
ul li a{
text-decoration: none;
}

@Dogukansvsl
Copy link

'Sefiller', 'auther' => 'Victor Hugo', 'releaseYear' => 1852, 'purchaseUrl' => 'https://www.dr.com.tr/Kitap/Sefiller-2-Cilt-Takim/Edebiyat/Roman/Dunya-Klasik/urunno=0000000651687', ], [ 'name' => 'Hayvan Çiftliği', 'auther' => 'George Orwell', 'releaseYear' => 1852, 'purchaseUrl' => 'https://www.dr.com.tr/kitap/hayvan-ciftligi/george-orwell/edebiyat/roman/dunya-roman/urunno=0000000105409', ] ]; ?>
    <ul> 
        <?php foreach ($books as $book) : ?>
            <li>
                
                <a href="<?= $book['purchaseUrl'] ?>">
                    <?= $book['name'] ?> (<?= $book['releaseYear']  ?>)
                </a>

            </li>
            <?php endforeach; ?> 
    </ul>

@JigokuPHP
Copy link

`

<title>Demo PHP</title>

Recommended Books

$books = [ ['name' => 'Keep it Up', 'dateRels'=> '1990', 'author' => 'brock Morris', 'buyUrl' => 'http://example.com'], ['name' => 'True GG', 'dateRels'=> '1960', 'author' => 'Haku mindra', 'buyUrl' => 'http://example-009.com'] ];

?>

  • ()
  • <?php endforeach; ?>
    
`

@hackershokirjonov
Copy link

 <?php
 $books = [
      [
           "name" => "Shum bola",
           "created_at"=> "nn.mm.aaaa",
           "author" => "Gafur Gulom",
           "purchaseUrl" => "https//kun.uz",
      ],
      [
           "name" => "Xamsa",
           "created_at"=>"xx.zz.yyyy",
           "author" => "Alisher Navoiy",
           "purchaseUrl" => "https//youtube.com",
      ]
 ]
 ?>

 <ul>
      <?php foreach ($books as $book) : ?>
           <a href="<?= $book['purchaseUrl'] ?>" style="text-decoration: none;">
                <li><?= $book['name'] . "| Yaratilgan sana:" . $book['created_at']  . "   | Yozuchi " . $book['author'] ?></li>
           </a>

      <?php endforeach;   ?>
 </ul>

@AdhiDevX369
Copy link

AdhiDevX369 commented Nov 26, 2023

<body>
    <?php
    $books = [
        [
            'name' => 'Asahara',
            'author' => 'Imesh Madhushanka Yapa',
            'urlToBuy' => 'www.buybooks.com/asahara',
            'releasedYear' => '2022'
        ],
        [
            'name' => 'Game Of thrones',
            'author' => 'George R. R. Martin',
            'urlToBuy' => 'www.buybooks.com/gameofthrones',
            'releasedYear' => '1996'
        ], [
            'name' => 'Forgot me or Not',
            'author' => 'Charitha Prawardhi Bandara, Chathuri Damayanthi',
            'urlToBuy' => 'www.buybooks.com/forgotmeornot',
            'releasedYear' => '2023'

        ], [
            'name' => 'Dark Matter',
            'author' => 'Blake Crouch',
            'urlToBuy' => 'www.buybooks.com/DarkMatter',
            'releasedYear' => '2016'

        ]
    ];
    ?>

    <ul>
        <?php foreach ($books as $book) : ?>
            <li>
                <a href="<?= $book['urlToBuy']; ?>">
                    <?= $book['name']; ?>
                </a>
                <?= $book['releasedYear']; ?>
            </li>
        (<?php endforeach; ?>)
    </ul>
</body>
</html>

@Shimazaky80
Copy link

My solution:

`

<title>Demo</title> <style> body { display: grid; place-items: center; /* height: 100vh; */ margin: 0; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; } </style>
<h1>Recommended Books</h1>

<?php
    // List of recommended books
    $books = [

        [
            "name" => "Do Androids Dream of Electric Sheep",
            "releaseYear" => 1968,
            "author" => "Philip K. Dick",
            "purchaseUrl" => "http://example.com"
        ],
        [
            "name" => "The Langoliers",
            "releaseYear" => 1995,
            "author" => "Andy Weir",
            "purchaseUrl" => "http://example.com"
        ],
        [
            "name" => "Project Hail Mary",
            "releaseYear" => 2021,
            "author" => "Jonh Doe",
            "purchaseUrl" => "http://example.com"
        ],
        [
            "name" => "Animal Farm",
            "releaseYear" => 1945,
            "author" => "Another Jonh Doe",
            "purchaseUrl" => "http://example.com"
        ]
        
    ];
?>

<ul>
    <?php foreach ($books as $book) : ?>
        <li>
            <a href="<?= $book["purchaseUrl"] ?>">
                <?= $book["name"] ?>
                (<?= $book["releaseYear"] ?>)
            </a>
        </li>
    <?php endforeach; ?>
</ul>
`

@spacemark3
Copy link

<?php $books = [ [ 'name' =>'Do Androids Dream of Electric Sheep', 'year' => '1980', 'author' => 'Philip K. Dick', 'purchaseUrl' =>'http://example1.com' ], [ 'name' => 'Project Hail Mary', 'year' => '2000', 'author' => 'Andy Weir', 'purchaseUrl' => 'http://example2.com' ], [ 'name' => 'The Langoliers', 'year' => '1950', 'author' => 'Mark Andro', 'purchaseUrl' => 'http://example3.com' ], [ 'name' => 'The shadow of the wind', 'year' => '2001', 'author' => 'Carlos Ruiz Zafon', 'purchaseUrl' => 'http://example4.com' ], ]; ?> <ul> <?php foreach($books as $book) : ?> <li> <a href="<?= $book['purchaseUrl'] ?>"> <?= $book['name']; ?> <?= $book['year']; ?> </a> </li> <?php endforeach; ?> </ul>

@K1LLERB0T
Copy link

<h1>Recommended Books</h1>
<?php
    $books = [
        [
            'name' => 'Do Androids Dream of Electric Sheep?',
            'author' => 'Phillip K. Dick',
            'releaseYear' => '1968',
            'purchaseUrl' => 'example.com'
        ],
        [
            'name' => 'The Langoliers',
            'author' => 'Stephen King',
            'releaseYear' => '1990',
            'purchaseUrl' => 'example.com'
        ],
        [
            'name' => 'Project Hail Mary',
            'author' => 'Andy Weir',
            'releaseYear' => '2021',
            'purchaseUrl' => 'example.com'
        ]
    ]
?>

<ul>
    <?php foreach ($books as $book) : ?>
        <li>
            <a href="<?= $book['purchaseUrl'] ?>">
            <?= $book['name']; ?>
            </a>
            <?= $book['releaseYear']; ?>
        </li> 
    <?php endforeach; ?>
</ul>

@Mickish1
Copy link

Mickish1 commented Apr 29, 2024

<title>FUN STUFFS</title>

Recommended List

'Do Androids Dream of Electric Sheep', 'author' => 'John Doe', 'purchaseUrl' => 'http://pavilion.com', 'releaseDate' => '2024' ], ['name' => 'The Extremist', 'author' => 'Michael Fasina', 'purchaseUrl' => 'http://pavilion.com', 'releaseDate' => '2021'], ['name' => 'Project Hail Mary', 'author' => 'Andy Weir', 'purchaseUrl' => 'http://fashmichael.com', 'releaseDate' => '2022' ] ] ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment