1

I have an array that was populated by this script :

var imgs = document.getElementsByTagName("img");
var imgSrcs = [];

for (var i = 0; i < imgs.length; i++) {
    imgSrcs.push(imgs[i].src);
}

When I echo this out from a PHP, it gives me all the img src with commas separating them. It seems like its not like a typical array format, more like just text. When I tried to do this:

for ($i = 0; $i < count($img); ++$i) {
    echo "<img src='".$img."'/><br>";
}

It just echo's one <img src with every single image src that is in imgSrcs.

I'm trying to echo img src and 1 img link. This way if I have 5 links in imgSrcs, i'll output 5 images.

Thanks in advance!

5
  • how are you passing the value of the array to php? therein lies your answer. Commented Jun 26, 2012 at 4:12
  • using ajax and into a php file $(isset($_POST['img'])) Commented Jun 26, 2012 at 4:12
  • where in ajax data: "img="+imgSrcs Commented Jun 26, 2012 at 4:13
  • And I dont think that matters, because when I just do alert(imgSrcs) it outputs exactly what I'm describing: list of img sources with comma separated Commented Jun 26, 2012 at 4:14
  • With "img="+imgSrcs you're trying to concatenate your array onto a string, which does a .toString() on imgSrcs resulting in a comma-separated string of the values from the array. Similarly, alert() is used to display strings, so alert(imgSrcs) displays your data with commas because it does a .toString() on your array. Commented Jun 26, 2012 at 4:33

1 Answer 1

1

Try explode to get your array back, if you're passing it as a comma delimited list.

$img = explode(",",$img);
for ($i = 0; $i < count($img); ++$i) {
    echo "<img src='".$img."'/><br>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

This turned it into Arrays, but it gave me a good start. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.