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 Associative Arrays

Andrew Zhao
Andrew Zhao
3,267 Points

Please help me resolve this error with my array...

I'm getting this error: " Notice: Array to string conversion in C:\xampp\htdocs\testsite.com\test.php on line 21 "

See code (please excuse messy testing code)

<?php
/*
$countries = array();
$countries["US"] = "United States"
$countries[]
*/

$country = array(
    "code" => "US",
    "name" => "United States",
    "capital" => "Washington, D.C.",
    "population" => 225000000,
    "anthem" => "The Star-Spangled Banner",
    "testolololo",  // < becomes indexed to $country[0]
);


?>


<?php $country[] = "this should come out when you call $country with key of 1";
?>

<br><br><br>Now I'm going to call $country[1], let's try it<br>
<?php echo $country[1]; ?>


<br><br><br>
<?php var_dump($country); ?>

IT works fine when I change $country[] = "" (on line 21) to almost any any other value!

But what's wrong with the string I have originally?

2 Answers

Alex Heil
Alex Heil
53,547 Points

hey Andrew Zhao , from a quick look at your code snippet I would say that you're hitting this error because of the $country that you have in the text. as you're using double quotes php will interpret this as a variable, while you actually only want text. remove the dollar sign and and run your code again, if this works fine then you know for sure where the problem came from.

hope that helps and have a nice day ;)

Andrew Zhao
Andrew Zhao
3,267 Points

Thank you again Alex :)

Alex Heil
Alex Heil
53,547 Points

you're welcome andrew, great to hear this helped you ;)