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 trial

PHP Build a Simple PHP Application Listing Inventory Items Displaying All Products

array 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 } ?>
Logan R
Logan R
22,989 Points

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

<?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&rsquo;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
Erik McClintock
45,783 Points

Giorgi,

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

But I am confused, Why teacher don't have any problem but I have

Erik McClintock
Erik McClintock
45,783 Points

I'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
Zeljko Porobija
11,491 Points

Notice is not something to be bothered with while learning PHP. Later on - in developing - notices are something that should be taken care of.