Naresh
Naresh

Reputation: 721

How to use php array in jquery

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 ?

Upvotes: 11

Views: 40503

Answers (6)

Rikesh
Rikesh

Reputation: 26421

You can use json_encode,

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

Upvotes: 23

Deepak Mane
Deepak Mane

Reputation: 115

Try this:

var jqueryarray = JSON.parse('<?php echo json_encode($myvalues); ?>');

Upvotes: 3

ssss
ssss

Reputation: 76

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

Upvotes: 0

Ryan
Ryan

Reputation: 14649

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>

Upvotes: 0

solvease
solvease

Reputation: 539

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>

Upvotes: 1

Sridhar R
Sridhar R

Reputation: 20408

<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); ?>

Upvotes: 3

Related Questions