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

Outputting Multiple Items from a PHP Associative Array

Hey guys,

Yet another simple PHP question as I can't seem to get hold of my 'go-to-guy'.

I'm trying to display the first and last name of a person (I'll use myself as the example), basically I have the array set up as seen below:

$player = array(
    "stu_cowley" => array('first_name' => 'Stu', 'last_name' => 'Cowley')
);

I would like to see both my first and last name, logically I would've thought you would simply do the following:

<?php echo $player['stu_cowley']['first_name']['last_name']; ?>

But I am left with the "White Screen of Death" when I use this method.

I would just make the array string "Stu Cowley" but I need to break people's name by their first name and last name in multiple sections of the site so I felt this was the best way to achieve this (if that makes sense).

If someone could give me some advice on how I could achieve this I would be incredibly grateful!

Stu :)

2 Answers

Stu,

You can achieve this with some simple concatenation.

Where you have:

<?php echo $player['stu_cowley']['first_name']['last_name']; ?>

...you should do:

<?php

echo $player['stu_cowley']['first_name'] . " " . $player['stu_cowley']['last_name'];

?>

Hope this helps!

Erik

Hey Erik McClintock

That worked perfectly, thanks for that! I totally forgot about concatenating.

Thanks heaps!

Stu :)

My pleasure!

Happy coding :)

Erik