11

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 ?

2
  • 1
    Use json_encode($myvalues); to convert your PHP array into JSON. Then you're able to assign it to your variable. Commented Apr 9, 2014 at 5:50
  • Possible duplicate of Get data from php array - AJAX - jQuery Commented Sep 23, 2016 at 21:16

6 Answers 6

23

You can use json_encode,

var jqueryarray = <?php echo json_encode($myvalues); ?>;
Sign up to request clarification or add additional context in comments.

6 Comments

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 - I added semi-colon on the end of the statement. It should work now also check the working demo I added.
thanks for ur help once again... In console its giving " TypeError: jqueryarray is null "
thanks boss its working now i just restarted my server once now its working fine
@Naresh - Have you check my demo ?
|
3
<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); ?>

Comments

3

Try this:

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

Comments

1

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>

Comments

0

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>

2 Comments

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...
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 "
0

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

Comments

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.