Last active
October 26, 2025 07:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
<?php
$books = [
[
'name' => "One Piece",
'author' => "Eiichiro Oda",
'purchaseUrl' => "http://example.com",
'releasedYear' => "1997"
],
[
'name' => "Inuyasha",
'author' => "Takahashi, Rumiko",
'purchaseUrl' => "http://example.com",
'releasedYear' => "1998"
]
];
?>
<ul>
<?php foreach ($books as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl']; ?>">
<?= "{$book['name']} ({$book['releasedYear']})"; ?>
</a>
</li>
<?php endforeach; ?>
</ul><?php
$books = [
[
'name' => "One Piece",
'author' => "Eiichiro Oda",
'purchaseUrl' => "http://example.com",
'releasedYear' => 1997
],
[
'name' => "Inuyasha",
'author' => "Takahashi, Rumiko",
'purchaseUrl' => "http://example.com",
'releaseYear' => 1998
]
];
?>
<ul>
<?php foreach ($books as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl']; ?>">
<?= "{$book['name']} ({$book['releaseYear']})"; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
'Sajjal Book',
'Author'=> 'Sajjal Hmgn',
'releaseYear' => 2007,
'purchaseUrl' => 'https://example.com'
],
[
'Name'=> 'Sajjal other Book',
'Author'=> 'Sajjal Hmgn2',
'releaseYear' => 2001,
'purchaseUrl' => 'https://example.com'
],
];
?>
Just trying this...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/styles.css" />
<link href="data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAzMzMA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAABAAEAEBAAAAEAAQAQEAAAAQABABAQAAABAAEAEBAAAAERAREQERAAAQEBABAQEAABAQEAEBAQAAEBAQAQEBAAAREBABAREAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" rel="icon" type="image/x-icon">
<title>PHP Sample</title>
</head>
<body>
<div class="container">
<div class="sub-container">
<h1>
<?php
$name = "The Dark Matter!";
$read = false;
if ($read) {
$message = "You have read $name";
} else {
$message = "You have not read $name";
}
echo $message;
?>
</h1>
<h2>Recommended Books</h2>
<?php
$books =
[
[
'name' => "The Orly Farm",
'author' => "John",
'releaseYear' => 2000,
'url' => "https://laracasts.com/"
],
[
'name' => "Magneto Revenge",
'author' => "Will Smith",
'releaseYear' => 1987,
'url' => "https://laracasts.com/"
]
];
?>
<div class="mini-container">
<ul>
<?php foreach ($books as $book) : ?>
<li>
<a href="<?= $book['url'] ?>" target="_blank">
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>)
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</body>
</html>
"Crossover",
"director" => "James Cameron",
"year" => 2010
],
[
"title" => "The Death Square",
"director" => "Fredrick Afful",
"year" => 2018
],
[
"title" => "The Hobbit",
"director" => "David Camelot",
"year" => 2017
],
[
"title" => "Fast X",
"director" => "Daniel Glover",
"year" => 2020
]
]
?>
<h2>Movies Recommendations (Year)</h2>
<ul>
<?php foreach ($Movies as $movie) :?>
<li><?= $movie['title']." (".$movie['year'].")"; ?></li>
<?php endforeach;?>
</ul>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Books</title>
</head>
<style>
body {
font-family: Arial, sans-serif;
}
container {
display: grid;
place-items: center;
}
</style>
<body>
<?php
$books = [
[
"id" => 1,
"cover" => null,
"name" => "Dark Matter",
"author" => "Blake Crouch",
"releaseYear" => 2016,
"purchaseUrl" => "http://example.com",
"summary" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel nisi nec velit consectetur dignissim. In non ipsum sed ipsum gravida tristique. Sed euismod, enim ut pulvinar cursus, nisi felis rutrum ex, id viverra justo felis non massa.",
"readed" => false,
"rating" => 4.8
],
[
"id" => 2,
"cover" => null,
"name" => "The Catcher in the Rye",
"author" => "J. D. Salinger",
"releaseYear" => 1951,
"purchaseUrl" => "http://example.com",
"summary" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel nisi nec velit consectetur dignissim. In non ipsum sed ipsum gravida tristique. Sed euismod, enim ut pulvinar cursus, nisi felis rutrum ex, id viverra justo felis non massa.",
"readed" => true,
"rating" => 4.8
],
[
"id" => 3,
"cover" => null,
"name" => "To Kill a Mockingbird",
"author" => "Harper Lee",
"releaseYear" => 1960,
"purchaseUrl" => "http://example.com",
"summary" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel nisi nec velit consectetur dignissim. In non ipsum sed ipsum gravida tristique. Sed euismod, enim ut pulvinar cursus, nisi felis rutrum ex, id viverra justo felis non massa.",
"readed" => false,
"rating" => 3.7,
],
];
?>
<div class="container">
<ul class="books list">
<?php foreach ($books as $book) :?>
<li id="book-<?= $book['id'];?>" class="book">
<div id="book-cover"><?= $book['cover']?></div>
<a class="book-name" href="<?= $book['purchaseUrl'];?>">
<?= $book['name'];?> (<?= $book['releaseYear'];?>)
</a>
<p class="book-summary"><?= $book['summary'];?></p>
<p class="book-rating">Rating: <?= $book['rating'];?></p>
<div class="book-controls">
<input type="checkbox" class="book-read-checkbox" <?= $book['readed']? 'checked' : '';?>>
<label for="book-read-checkbox">Read</label>
</div>
</li>
<?php endforeach;?>
</ul>
</ul>
</div>
</body>
</html><!DOCTYPE html>
<head>
<title>Learn PHP</title>
<style>
body {
display: grid;
place-Items : center;
height: 100vh;
font-size: 25px;
margin: 0;
}
</style>
</head>
<body>
<?php
$books = [
[
'name' => "Masud Rana",
'author' => "Kazi Anowar Hossain",
'purchaseUrl' => "https://example.com"
],
[
'name' => "Tin Goyenda",
'author' => "Shamsuddin Nawab",
'purchaseUrl' => "https://example2.com"
]
];
?>
<ul>
<?php foreach($books as $book) : ?>
<li>
<h3>Name: <?= $book["name"] ?></h3>
<p>Author: <?= $book["author"] ?> </p>
<a href="<?= $book['purchaseUrl'] ?>">
Buy Now
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html><?php
/* __________________________________________________
Keys and Values
Echo the value that is associated with the book's name key.
*/
$book = [
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'purchaseUrl' => 'http://example.com'
];
echo $book['name'];
/* __________________________________________________
Update an Array
We've prepared an array of two books. Your job is to extend each item to include the book's release year. Use releaseYear for the key. Get to work!
*/
// "Do Androids Dream of Electric Sheep" was released in 1968.
// "Project Hail Mary" was released in 2021.
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'purchaseUrl' => 'http://example.com',
'releaseYear' => 1968,
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'purchaseUrl' => 'http://example.com',
'releaseYear' => 2021,
]
];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Recommended Books