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 trialGiorgi Kiknadze
Courses Plus Student 2,768 Pointsarray to string conversion ! Need Help!
I have problem, I have done everything right but there is an error message: notice: array to string conversion in /applications/mamp/htdocs/shirts4mike/shirts.php on line 61 array
Here is 61 Line
<?php foreach($products as $product) {?>
<li><?php echo $product ; ?></li>
<?php } ?>
Giorgi Kiknadze
Courses Plus Student 2,768 Points<?php
$products = array();
#Adds new item in the 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,
);
?>
<?php
$page_title = "Mike's Full Catalog of Shirts";
$section = "shirts";
include('includes/header.php');
?>
<div class="section shirts page">
<div class="wrapper">
<h1>Mike’s Full Catalog of Shirts</h1>
<ul class="products">
<?php foreach($products as $product) {?>
<li><?php echo $product ; ?></li>
<?php } ?>
</ul>
</div>
</div>
<?php include('includes/footer.php'); ?>
2 Answers
Erik McClintock
45,783 PointsGiorgi,
When echoing what you're echoing here, that is to be expected. Echo's intent is to print out one or more strings, but the value stored in your working $product variable is of type array, so all that should get you is the fact that it's an array, and then the notice that you see there. As you continue through the video that you're on/the course, you'll learn the proper way to access individual values within that array, which will then help you to print more useful information :)
Hope this helps!
Erik
Giorgi Kiknadze
Courses Plus Student 2,768 PointsBut I am confused, Why teacher don't have any problem but I have
Erik McClintock
45,783 PointsI'm not sure; he may have errors and notices/warnings turned off in his php.ini file, perhaps. It shouldn't be of any concern, though! Things look fine in your code, and again, that is expected behavior there.
You should be good to press on!
Erik
Zeljko Porobija
11,491 PointsNotice is not something to be bothered with while learning PHP. Later on - in developing - notices are something that should be taken care of.
Logan R
22,989 PointsLogan R
22,989 PointsYour error means that what you are trying to print out isn't a string, but an array.
Can you please edit your post to include the entire PHP file or add a comment with it, and not just the for loop?
Thanks!