Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active March 24, 2026 10:45
Show Gist options
  • Select an option

  • Save JeffreyWay/40fcd41c03de5805aedc62ac847094df to your computer and use it in GitHub Desktop.

Select an option

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>
@Nixsoft-ict
Copy link
Copy Markdown

$books = [
[
"name" => "Purple Hibiscus",
"author" => "Chimamanda Ngozi Adichie",
"bookUrl" => "https://bordersliteratureonline.net/books/purple-hibiscus"
],
[
"name" => "New Daughters of Africa",
"author" => "Margaret Busby",
"bookUrl" => "https://bordersliteratureonline.net/books/New-Daughters-of-Africa"
],
[
"name" => "Self in the World: Connecting Life's Extremes",
"author" => "Keith Hart",
"bookUrl" => "https://bordersliteratureonline.net/books/Keith_Hart"
]
];
foreach ($books as $book):
echo "{$book["name"]} ". "
";
echo "{$book["author"]} ". "
";
echo "{$book["bookUrl"]} ". "
";
endforeach;

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