Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmckbas
14,166 PointsOutput an array to page
Hello all!
I'm a bit stumped. I haven't worked with PHP much before but surely there has to be a way to accomplish what I want to do.
I have an array (let's call it $array) and I want to use PHP to "read" through my HTML and find all instances of %replaceme% so I can replace all of them with values from my array.
//Before
$array = ["name","date","location"];
<body>
<h1>%replaceme%</h1>
<h2>%replaceme%</h2>
<ul>
<li>%replaceme%</li>
</ul>
</body>
//After
$array = ["name","date","location"];
<body>
<h1>name</h1>
<h2>date</h2>
<ul>
<li>location</li>
</ul>
</body>
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! I'm not sure exactly what you're asking. PHP is primarily used to generate HTML. That being said, once it's generated and rendered, you'd likely use JavaScript to alter the DOM. You might be thinking of a web template? I'm not sure here. Here's what I would do:
<h1><?php echo $array[0]; ?></h1>
<h2><?php echo $array[1]; ?></h2>
Hope this helps!
jamesjones21
9,260 Pointsas Jennifer mentioned, using the variable $array, all we would have to do is note the key of each of the items in the array to replace the %replaceme%. $array[key].