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

sebastian rojas
sebastian rojas
571 Points

Array to string conversion error

hey! im trying to code some via the videos and im getting an error at line 59

<?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,
);


?><?php 
$pageTitle = "Mike's full Catalog of shirts";
$section = "shirts";
include('include/header.php'); ?>

    <div class ="section page">

      <div class="wrapper">

         <h1>Mike&rsquo;s full catalog of shirts</h1>

         <ul>
            <?php foreach($products as $product) {?>
               <li><?php echo $product; ?></li>
            <?php } ?>
         </ul>



         </div>

</div>

<?php include ('include/footer.php'); ?>

this is where i get my error

   <h1>Mike&rsquo;s full catalog of shirts</h1>

         <ul>
            <?php foreach($products as $product) {?>
               <li><?php echo $product; ?></li>
            <?php } ?>
         </ul>

Notice: Array to string conversion in C:\xampp\htdocs\COMPTURBO\shirts.php on line 59 Array

Tibor Ruzinyi
Tibor Ruzinyi
17,968 Points

Hi , which piece of code did you write wrong ? Iam having the same problem :)

2 Answers

Yes, but his $products array contains strings as elements, while your array (the one you've pasted) contains other arrays as elements, so you need to provide an additional key to specify which part of the inner array you want to access/output.

Ah, I see what you mean. Based on the version of PHP it might not be possible to have the $echo construct output Array when you use an array as an argument, so that's what's causing the error. If you try accessing an element via key directly (as in the example I posted), it should work. Or you could pass the array to a function like implode() first, to convert it into a string.

sebastian rojas
sebastian rojas
571 Points

I tried out the code as you said and it worked! :)

But i had not paid enough notice to the video so i missed the code that he wrote so it all worked out! hehe :)

The elements inside the $products array are arrays themselves, so you should specify which element (via key) you want to output:

<?php echo $product["name"]; ?>
sebastian rojas
sebastian rojas
571 Points

Well, im trying to follow this video http://teamtreehouse.com/library/build-a-simple-php-application/listing-inventory-items/displaying-all-products

and he haven't done a such thing and it is working for him so im abit confused atm :/