0

I have this code that executes a for loop in javascript with a php array inside of it. Is there anyway I can use the variable for the loop inside of the php variable for example. This is all inside of php.

echo '<script>
for (var i =0; i<4;i++){

alert("hey"'.$phparr[i].');



}</script>';

I know this will not work because the $phparr is a php variable while the i is a javascript variable is there anyway I can do this?

1
  • 1
    There are a lot of downvotes on answers in this question. Is someone taking them personally? Commented Apr 12, 2012 at 0:50

5 Answers 5

0

Try:

<script>    
    var phparr = <?php echo json_encode($phparr); ?>;

    for (i in phparr){

        alert("hey" + phparr[i]);

    }
</script>

PHP is a server side language which means it executes before it reaches the client (browser).

JavaScript is a client side language which means it is executed in the browser (client side).

The best tool for a new web developer is Google search. Learn how to search effectively.

1
  • I used for (i in phparr) because I have no idea what your keys are and how many there are. Commented Apr 12, 2012 at 0:35
0

You're almost there. In order to access the php array in javascript, you first have to echo out the php array into a javascript array. Try:

$phparr_imploded = implode(',',$phparr);
echo '
    <script>
        var arr = ['.$phparr_imploded.'];
        for (var i =0; i<4;i++){
            alert("hey"+arr[i]);
        }
    </script>
';

If your php array is coming from a database, or contains special characters that javascript may misinterpret, make sure to sanitize before outputting.

Are the objects in your php array strings? If so, you need to surround the objects with escaped quotations BEFORE the implode.

for($i=0; i<count($phparr); $i++){
    $phparr[i] = '"'.$phparr[i].'"';
};
2
  • Hey this worked great however the strings contained in my array are actually html. I have for example an array of <option value = 'someting'>something</option>. Right now it is just something with out the quotes because otherwise javascript will not take it. Is there anyway to be able to have those in there otherwise I can't print my options inside of my javscript select. Commented Apr 12, 2012 at 3:30
  • Ah, well remember there are 2 types of quotes, single and double quotes. When working with 3 different languages at once (html, javascript, and php) it can get tricky. Based on the code you put earlier, the alerts should work. I assume that its not the code you're struggling with at the moment. Commented Apr 18, 2012 at 5:36
-1

How about storing the PHP array in a Javascript variable and looping through as kpotehin suggests?

<script>
var i = 0, name;
var myArr = <?php echo json_encode($my_array); ?>;

while (name = myArr[i++]){
  alert("hey " + name);
}
</script>
1
  • (this assumes that $my_array holds the array in PHP) Commented Apr 12, 2012 at 0:21
-1

You should make it this way:

var arr = ["<?php echo implode('","',$array); ?>"];
for (var i =0; i<4;i++){
alert("hey"+ arr[i]);
}
1
  • Using json_encode is typically better than doing something like that. The reason why is that what if one of your $array elements contains a quote? Commented Apr 12, 2012 at 0:25
-1

Your basic problem is that you are forgetting that the PHP code and the JavaScript code do not execute at the same time. The PHP runs to completion, outputing HTML and JavaScript. Then the browser runs the JavaScript. If the JavaScript needs dynamic access to data in a PHP variable (including an array), then the PHP needs to generate JavaScript declaring the data in a JavaScript structure. That is what all the other answers are doing.

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.