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>
@ta-riq
Copy link
Copy Markdown

ta-riq commented Feb 20, 2025

<!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>

@MatimotTheTimoters
Copy link
Copy Markdown

<?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,
	]
];

@AdilAzhari
Copy link
Copy Markdown

<!doctype html>

<title>Demo</title> 'Do Androids Dream of Electric Sheep', 'author' => 'Philip K. Dick', 'purchaseUrl' => 'http://example.com', 'releaseYear' => 2020, ], [ 'name' => 'Project Hail Mary', 'author' => 'Andy Weir', 'purchaseUrl' => 'http://example.com', 'releaseYear' => 1992, ] ]; ?>
<ul>
    <?php foreach ($books as $book) : ?>
        <li>
            <a href="<?= $book['purchaseUrl'] ?>">
                <?= $book['name'] . " " . $book['releaseYear']?>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

@djoooles
Copy link
Copy Markdown

<title>Arrays</title>

Recomended Books

<?php 

    $books = [
        [
            'name' => 'Do Andoroids Dream of Electric Sheep',
            'autor' => 'Philip K. Dick',
            'purchaseUrl' => 'http:/example.com'
        ],
        [
            'name' => 'Project Hail Marry',
            'autor' => 'Andy Weir',
            'purchaseUrl' => 'http:/example.com'
        ],
    ];
?>

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

@yanji12-droid
Copy link
Copy Markdown

<style> body{ display: grid; place-items: center; font-size: 30px; padding-top: 200px; font-style: italic; } body ul{ background-color: grey;
}
</style>
'Babur-Nama', 'Author' => 'Babur', 'PurchaseUrl' => 'http://example.com', 'ReleaseYear' => 2005 ], [ 'name' => 'Bahay Kubo', 'Author' => 'Gian', 'PurchaseUrl' => 'https://genius.com/Jona-bahay-kubo-lyrics', 'ReleaseYear' => 2000 ]
];

?>

      <li>   <a href=<?= $book['PurchaseUrl'] ?> >
        <?= $book['name'] ?>
       </li>
    
    Release Date:
             <li>
                Author:
                <?= $book['Author'] ?>
             </li>
    

@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