Created
May 23, 2016 22:56
-
-
Save tomstuder/298396022314ead1106235e090bb76d3 to your computer and use it in GitHub Desktop.
Example of using foreach with a multidimensional array
This file contains 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
<?php | |
$posts = array ( | |
array ( | |
title => "First Post", | |
author => "Tom Studer", | |
category => "design", | |
post => "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque in fugit rem dolore exercitationem saepe. Velit nemo tempora, repudiandae eveniet assumenda. Beatae debitis modi id dolorum molestias repudiandae culpa neque." | |
), | |
array ( | |
title => "Second Post", | |
author => "Tom Studer", | |
category => "illustration", | |
post => "Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vestibulum id ligula porta felis euismod semper. Maecenas faucibus mollis interdum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum." | |
) | |
); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>MD-ARRAY</title> | |
</head> | |
<body> | |
<div> | |
<?php | |
foreach ($posts as $post => $p) { | |
echo '<h1>' . $p[title] . '</h1>'; | |
echo '<p class="author">' . "author: " . $p[author] . '</p>'; | |
echo '<p class="category">' . "category: " . $p[category] . '</p>'; | |
echo '<p>' . $p[post] . '</p>'; | |
} | |
?> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment