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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Ryann Green](https://uploads.teamtreehouse.com/production/profile-photos/1189122/micro_avatar_be16a8dc0855_128.png)
Ryann Green
11,010 PointsIncluding PHP Array within Javascript
Right now I have a series of object in an include PHP file like this:
$shoots[01] = array(
"name" => "Rustic Farm Shoot",
"id" => "rustic",
"img" => array("img/slider.jpg", "img/testbw2.jpg")
);
$shoots[02] = array(
"name" => "Bridesmaids' Party",
"id" => "bridesmaids",
"img" => array("img/testcolor.jpg")
);
What I am trying to do is include the img array items as a javascript variable array. What is making this difficult is that I would like the javascript array to change dynamically depending on what page it is. I'm doing something similar to display an image like this:
<?php foreach($shoots as $shootID => $shoot) {
if($_GET["id"] == $shootID) {
foreach($shoot['img'] as $img) break; ?>
<img src="<?php echo $img; ?>" alt="<?php echo $shoot['name']; ?>">
<?php }}; ?>
The javascript is below that section. So basically, I would like something similar that if the $_GET["id"] == $shootID, then it should use that object's "img" array. Hopefully this makes sense. Any help is appreciated.
3 Answers
![miguelcastro2](https://secure.gravatar.com/avatar/3c4334acc0e6ee2140d33d2d72fc643c?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
miguelcastro2
Courses Plus Student 6,573 PointsMaybe if you post your Javascript it might make more sense.
![Travis Roemer](https://secure.gravatar.com/avatar/641f5828359ff3a7e67664308ac34004?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Travis Roemer
1,847 PointsI would just use php and forget javascript all together, unless you are trying to make slider dynamic, then just insert you foreach loop into javascript.
<?php
$shoots[01] = array(
"name" => "Rustic Farm Shoot",
"id" => "rustic",
"img" => "images/Facebook.png"
);
$shoots[02] = array(
"name" => "Bridesmaids' Party",
"id" => "bridesmaids",
"img" => "images/Twitter.png"
);
if(isset($_GET["id"])) {
$id = $_GET['id'];
}else{
$id = "";
}
?> <?php foreach($shoots as $shootID => $shoot) { ?>
<?php echo "<a href=\"test.php?id=".$shootID." \">{$shoot['name']}</a>"; ?>
<?php
if($id == $shootID){
?>
<img src="<?php echo $shoot['img']; ?>" alt="<?php echo $shoot['name']; ?>">
<?php }}?>
```
![Ryann Green](https://uploads.teamtreehouse.com/production/profile-photos/1189122/micro_avatar_be16a8dc0855_128.png)
Ryann Green
11,010 PointsSorry for the lack of responses. I figured out what I was doing wrong and just wanted to post it in case anyone had the same question. This is the javascript I used in addition to the code listed above.
<script>
images = [<?php foreach($shoots as $shootID => $shoot) {
if($_GET["id"] == $shootID) {
foreach($shoot['img'] as $img) {
echo json_encode($img, JSON_UNESCAPED_SLASHES) . ",";}}} ?>];
</script>
Thanks for all the help. If anyone knows a better or different way to do the same thing, I would be interested to see it. I'm sure this isn't the most efficient way.