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 PHP Arrays and Control Structures PHP Arrays Multidimensional Arrays

Donald Mak
Donald Mak
1,603 Points

What's wrong with my code? Is that an html error?

I'm supposed to echo the string values stored under 'name' and 'email' keys of each of the arrays (0/1/2/3) under the array $contacts. I don't know why couldn't the values be shown? The system said problem was "array to string conversion" but I don't understand

index.php
<?php
//edit this array
$Alena=array('name'=>'Alena Holligan','email'=>'alena.holligan@teamtreehouse.com');
$Dave=array('name'=>'Dave McFarland','email'=>'dave.mcfarland@teamtreehouse.com');
$Treasure=array('name'=>'Treasure Porth','email'=>'treasure.porth@teamtreehouse.com');
$Andrew=array('name'=>'Andrew Chalkley','email'=>'andrew.chalkley@teamtreehouse.com');
$contacts = array($Alena, $Dave, $Treasure, $Andrew);


echo "<ul>\n";
//$contacts[0] will return 'Alena Holligan' in our simple array of names.
echo "<li>$contacts[0]['name'] : $contacts[0]['email']</li>\n";
echo "<li>$contacts[1]['name'] : $contacts[1]['email']</li>\n";
echo "<li>$contacts[2]['name'] : $contacts[2]['email']</li>\n";
echo "<li>$contacts[3]['name'] : $contacts[3]['email']</li>\n";
echo "</ul>\n";

1 Answer

Mike Bronner
Mike Bronner
16,395 Points

This works:

<?php
    $Alena=array('name'=>'Alena Holligan','email'=>'alena.holligan@teamtreehouse.com');
    $Dave=array('name'=>'Dave McFarland','email'=>'dave.mcfarland@teamtreehouse.com');
    $Treasure=array('name'=>'Treasure Porth','email'=>'treasure.porth@teamtreehouse.com');
    $Andrew=array('name'=>'Andrew Chalkley','email'=>'andrew.chalkley@teamtreehouse.com');
    $contacts = array($Alena, $Dave, $Treasure, $Andrew);


    echo "<ul>\n";
    //$contacts[0] will return 'Alena Holligan' in our simple array of names.
    echo "<li>{$contacts[0]['name']} : {$contacts[0]['email']}</li>\n";
    echo "<li>{$contacts[1]['name']} : {$contacts[1]['email']}</li>\n";
    echo "<li>{$contacts[2]['name']} : {$contacts[2]['email']}</li>\n";
    echo "<li>{$contacts[3]['name']} : {$contacts[3]['email']}</li>\n";
    echo "</ul>\n";
?>

See this SO discussion for details: https://stackoverflow.com/questions/35598187/when-to-wrap-curly-braces-around-a-variable

Donald Mak
Donald Mak
1,603 Points

Great! Thanks Mike! Is adding { } something related to html or php syntax?

Mike Bronner
Mike Bronner
16,395 Points

It's a PHP thing. I think it used to work in PHP 5 without them, and you might need them now in PHP7, but not 100% sure if that's the cause or not. I know that the curly braces are used for dynamic variable scoping in PHP 7, for example:

$cars->$type['automatic'] // evaluated as ($cars->type)['automatic']
// is different than
$cars->{$type['automatic']} // evaluated as $cars->($type['automatic'])

(see Uniform Variable Syntax: https://blog.digitalocean.com/getting-ready-for-php-7/)