Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

you can see my goal here,

i have a php array in my code like

<?php $myvalues = array(13,45,23,54,767,234,543,245); ?>

i want to store all the values in jquery array in my script

<script>
    $(document).ready(function(){
      var jqueryarray = $myvalues; // here i want to store $myvalues array values in jqueryarray
        for (var i = 0; i < jqueryarray.length; i++) {
            //my stuf
        };
    }); 
</script>

How can i do this one ?

Any ideas ?

share|improve this question
    
Use json_encode($myvalues); to convert your PHP array into JSON. Then you're able to assign it to your variable. –  Padarom Apr 9 at 5:50

6 Answers 6

up vote 5 down vote accepted

You can use json_encode,

var jqueryarray = <?php echo json_encode($myvalues); ?>;

Working Demo.

share|improve this answer
    
thanks for the reply if i put alert(jqueryarray); its giving " object object " in alert box if i run the for loop for this is like this for (var i = 0; i < jqueryarray.length; i++) { alert(jqueryarray[i]); }; its not giving any alert message for for loop i hope for that means jqueryarray is empty..but if i alert(jquery.length); this one also not working... –  Naresh Apr 9 at 6:13
    
@Naresh - I added semi-colon on the end of the statement. It should work now also check the working demo I added. –  Rikesh Apr 9 at 6:18
    
thanks for ur help once again... In console its giving " TypeError: jqueryarray is null " –  Naresh Apr 9 at 6:22
    
thanks boss its working now i just restarted my server once now its working fine –  Naresh Apr 9 at 6:25
    
Glad to help you. Kindly accept the answer if your problem is solved by this. –  Rikesh Apr 9 at 6:25
<script type='text/javascript'>
<?php
$php_array = array(13,45,23,54,767,234,543,245); 
$js_array = json_encode($php_array);
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>

OR

var jqueryarray = <?php echo json_encode($myvalues); ?>

share|improve this answer

Pretty simple and you were almost there. Use the below code and of course in php file

<?php $myvalues = array(13,45,23,54,767,234,543,245); ?>
<script>
    $(document).ready(function() {
        var jqueryarray = <?php echo json_encode($myvalues ); ?>;
        for (var i = 0; i < jqueryarray.length; i++) {
            console.log(jqueryarray[i]);
        }
        ;
    });
</script>
share|improve this answer
    
Thanks for the reply .... its not displaying anything in console. –  Naresh Apr 9 at 6:11

Use json_decode to turn it into a JSON string. (an object in JavaScript), then traverse the object in JS. Traversing an array in JavaScript is not much different from doing it in PHP. All you have to do is pluck the array from the JSON object and then you can use a regular for loop.

<?php $myvalues = array(13,45,23,54,767,234,543,245); ?>
<script>
  var myObject = <?php echo json_encode(array("payload" => $myvalues)); ?>;
  var payload = myObject.payload;
  /*
  [ 13,
    45,
    23,
    54,
    767,
    234,
    543,
    245 ]
   */

  for(var i = 0; i < payload.length; i++) {
    alert(payload[i];
  }
</script>

Using Array.prototype.forEach you can also traverse the object with an elegant callback function which gets 3 values: the value of the array at the current enumeration, the current numeric index, and a reference to array itself. Using this you wouldn't have to declare any iteration variables.But that probably won't work in some versions of IE if you're looking for a cross-browser solution.

<script>
   var myObject = <?php echo json_encode(array("payload" => $myvalues)); ?>;
   var payload = myObject.payload;
   payload.forEach(function(val, index, array) {
     alert(array[index]);
   });
</script>

There are also other ways to traverse the object:

<script>
   var myObject = <?php echo json_encode(array("payload" => $myvalues)); ?>;
   var payload = myObject.payload;
   var node;
   while(node = payload.shift()) {
     alert(node);
   }
</script>
share|improve this answer
    
thanks for the reply i tried these two even its not giving alert(payload.length); for this one also....& its giving " TypeError: payload is null " & i gave one alert("hi"); its working... –  Naresh Apr 9 at 6:06
    
Did you echo out the result of json_decode? –  true Apr 9 at 6:11
    
yeah.. i tried like alert(myobject); its giving "object object" in alert box, if i give alert(payload.length); even alert message also not coming... & in console its giving " TypeError: payload is null " –  Naresh Apr 9 at 6:17

You can use php variables in javascript using JSON_ENCODE() ...

share|improve this answer

Try this:

var jqueryarray = JSON.parse('<?php echo json_encode($myvalues); ?>');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.