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 trialPhilip Harper
10,033 PointsHow 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
2 Answers
Stephen Van Delinder
21,457 PointsHey 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
11,933 PointsCheck out the explode function.
Philip Harper
10,033 PointsPhilip Harper
10,033 PointsAh! 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.