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 trialJerôme Naglé
14,189 PointsQuestion about this video
When I add the .txt file into my shirts.php I get an error in the foreach loop.
Notice: Array to string conversion
Can you please help me
This is my code:
<?php
$products = array();
$products[101] = array( "name" => "Logo Shirt, Red", "img" => "img/shirts/shirt-101.jpg", "price" => 18 ); $products[102] = array( "name" => "Mike the Frog Shirt, Black", "img" => "img/shirts/shirt-102.jpg", "price" => 20 ); $products[103] = array( "name" => "Mike the Frog Shirt, Blue", "img" => "img/shirts/shirt-103.jpg", "price" => 20 ); $products[104] = array( "name" => "Logo Shirt, Green", "img" => "img/shirts/shirt-104.jpg", "price" => 18 ); $products[105] = array( "name" => "Mike the Frog Shirt, Yellow", "img" => "img/shirts/shirt-105.jpg", "price" => 25 ); $products[106] = array( "name" => "Logo Shirt, Gray", "img" => "img/shirts/shirt-106.jpg", "price" => 20 ); $products[107] = array( "name" => "Logo Shirt, Turquoise", "img" => "img/shirts/shirt-107.jpg", "price" => 20 ); $products[108] = array( "name" => "Logo Shirt, Orange", "img" => "img/shirts/shirt-108.jpg", "price" => 25, );
$pageTitle = 'Shirts Mike'; $section = 'shirts';
?>
<?php include('inc/header.php'); ?>
<div class="section page">
<ul>
<?php
foreach ($products as $product) {
?>
<li><?php echo $product; ?></li>
<?php } ?>
</ul>
<h1>Full catalog of shirts</h1>
</div>
<?php include('inc/footer.php'); ?>
3 Answers
Agapito Cruz
21,486 PointsHello Jerome,
The $product variable holds an array of values that describes each shirt. PHP is saying that you are attempting to treat the array as a string when you use the echo command. I'm assuming you want to print the shirt's name, so you should replace the
<ul>
<?php
foreach ($products as $product) {
?>
<li><?php echo $product; ?></li>
<?php } ?>
</ul>
with
<ul>
<?php
foreach ($products as $product) {
?>
<li><?php echo $product["name"; ?></li>
<?php } ?>
</ul>
I think this will help with your error messae.
I hope this helps,
-Agapito
Jerôme Naglé
14,189 Pointsthanks!
Jerôme Naglé
14,189 Pointsthanks!