tags:

views:

158

answers:

4

Hi guys, For example I have a PHP array, such as this one

<?php $s= array('a','b','c','d','e','f') ; ?>

And I need to loop through it in JavaScript, any ideas how do I do that?

for ( i=0 ; i < <?php echo sizeof($s) ?> ; i++) {
document.write('<?php echo $s [somehow need to get the 'i' value into here] ?>');
}

Any suggestions? Thanks!

+3  A: 

Yes.... echo out your PHP array as a JavaScript array first, and then loop over that. Don't try looping over your PHP array; you can't.

Mark
a... how do I echo out my PHP array into a JS array?
`json_encode()` would be a good start, if it's available.
ZeissS
this is so sad :D
Joel L
+1  A: 
<?php
$s= array('a','b','c','d','e','f') ;
?>

<?php foreach($s as $a){ ?>

document.write('<?=$a?>');

<?php } ?>

Not tested but thats one way.

zaf
thanks man, it works that way!
var lat=new Array();var long=new Array();<?php foreach($latlong as $a){ ?> lat.push('<?php echo $a[0] ?>'); long.push('<?php echo $a[1] ?>'); <?php } ?>
I *really* hope you're not *just* printing out the vars... (you have `echo` for that)
Mark
+3  A: 

Javascript and PHP cannot be combined. They are two completely different programs that communicate only vaguely. The PHP runs on the server computer and generates the HTML. The javascript runs on the client computer in the webbrowser and acts on that HTML. If you need to move information from PHP into Javscript somehow, then you have to store it in the HTML and have the Javascript access it through that HTML. If you need to do the reverse, move information from Javascript to PHP, have the Javascript call a PHP page with a query string.

One way to place the information in your array somewhere where Javascript can get to it, would be to echo it into a hidden div. Either in a series of ided spans or just a comma separated list. Then you can pull it out of the DOM.

For example:

<div style="display: none;" id="myArray">
<?php 
echo '<span id="myArray.count">'.sizeof($s).'</span>';
for ($i = 0; $i < sizeof($s); $i++) {
    echo '<span id="myArray.'.$i.'">'.$s[$i].'</span>';
}
?>
</div>

Then in the Javascript you can access the array in the DOM:

var myArray = new Array();

for(i = 0; i < document.getElementById('myArray.count').innerHTML; i++) {
  document.write(document.getElementById('myArray.'+i).innerHTML);
}

Disclaimer: untested code, and I don't have the time to perfect it right now. If someone else wants to comment or edit to fix any errors feel free :)

Daniel Bingham
"communicate vaguely", huh? PHP is a templating technology, JS is template text like HTML/CSS. Just let PHP print JS as-is and then let the webbrowser do the work of interpreting/executing JS. You don't necessarily need to print it to a hidden div or so. Just for example `<script>var x = '<?= $x; ?>';</script>` is enough to assign a PHP variable as if it is a JS variable.
BalusC
+3  A: 

Before your ehco/print or else your php array we make sure it's in JavaScript syntax.

<?php
$s=array('a','b','c','d','e','f');
$s_to_json=json_encode((array)$s);
?>

<script type="text/javascript">

var fromPHP=<? echo $s_to_json ?>;

for (i=0; i<fromPHP.length; i++) {

yourValue=fromPHP[i];

}

</script>
Zlatev