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 ?

Naresh
  • 681
  • 2
  • 12
  • 39
  • 1
    Use `json_encode($myvalues);` to convert your PHP array into JSON. Then you're able to assign it to your variable. – padarom Apr 09 '14 at 05:50
  • Possible duplicate of [Get data from php array - AJAX - jQuery](http://stackoverflow.com/questions/6395720/get-data-from-php-array-ajax-jquery) – T.Todua Sep 23 '16 at 21:16

6 Answers6

22

You can use json_encode,

var jqueryarray = <?php echo json_encode($myvalues); ?>;
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • 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 09 '14 at 06: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 09 '14 at 06:18
  • thanks for ur help once again... In console its giving " TypeError: jqueryarray is null " – Naresh Apr 09 '14 at 06:22
  • thanks boss its working now i just restarted my server once now its working fine – Naresh Apr 09 '14 at 06:25
  • @Naresh - Have you check my demo ? – Rikesh Apr 09 '14 at 06:28
  • I see how you could use ``BUT this only works if the javascript is written inside the php file containing the `$myvalues` Array. How would you go about it, if the javascript is an extra file which is imported inside the html `head` or `footer`doesnt matter. You cant use `` in an external JS file. Is there still a way to access the `$myvalues` Array? I can only think of either sending it to the JS file via `GET`or `POST`or storing the Array data to some dummy html tag as attributes and than reading the attributes in, within the JS. Any other suggestions? – b101 Apr 14 '18 at 12:02
3

Try this:

var jqueryarray = JSON.parse('<?php echo json_encode($myvalues); ?>');
Jeroen
  • 60,696
  • 40
  • 206
  • 339
Deepak Mane
  • 105
  • 6
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); ?>

Sridhar R
  • 20,190
  • 6
  • 38
  • 35
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>
solvease
  • 529
  • 2
  • 11
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>
Ryan
  • 14,392
  • 8
  • 62
  • 102
  • 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 09 '14 at 06:06
  • 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 09 '14 at 06:17
0

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

ssss
  • 76
  • 8