Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

How do I echo only parts of an array in PHP?

Hey gang,

I've got this link here...

<a href="museum-pages/British-Dental-Association-Museum.php" id="" title="British-Dental-Association-Museum">British Dental Association Museum</a>

echo'd from a database, I know the result from a database query comes back as an array, but how do I echo only this part of the link...

"museum-pages/British-Dental-Association-Museum.php"

? I'm sure there is a function for it, but not sure where to look?

I'd be really grateful for your help!

Cheers,

Philip

Ah! Forgot to add this...!

           <a href="museum-pages/British-Dental-Association-Museum" id="" title="British-Dental-Association-Museum">British Dental Association Museum</a>

So again, all I need to echo is"museum-pages/British-Dental-Association-Museum.php" ....

Sorry, that's such a schoolboy error. Apologies.

2 Answers

Hey Philip,

It's hard to say without seeing the code and the explode function is helpful for sure. You can also just grab things from an array by adding the index number (starting at zero) of what you want to include in the link.

Here's a simple example:

<?php 
$array = ["hyperlink text","example_domain","three","four"]; 
echo "<a href=http://" . $array[1] . ".com>" . $array[0] . ".</a>"; 
?>

Hope this helps!

Sean Do
Sean Do
11,933 Points

Check out the explode function.

http://php.net/explode