0

I have an array in php lets call it:

$nums = array(10,25,52,32,35,23);

I want to send it to my javascript function like this:

var nums=[10,25,52,32,35,23];

How can i do this? (My javascript file name is "nums.js")

Edit:

nums.js is a javascript code. It changes the values of table in the html. But the values only exist in php. So i have to send the values to javascript.

4 Answers 4

2

PHP >= 5.2

The json_encode function is for this purpose:

<?php echo 'var nums=' . json_encode(array(10,25,52,32,35,23)) . ';'; ?>

Docs: http://php.net/json_encode

PHP < 5.2

If you can't use json_encode, take a look at this post for a way to define an equivalent.

4
  • This prints the array into my web page. But dont change the values in my javascript. My code is like this: <?php $nums = array(10,25,52,32,35,23); ?> <script src="nums.js"></script> I want to change the values inside the nums.js file. Commented Feb 11, 2014 at 23:46
  • 1
    @user3179249 easiest way is add new script tag before external javascript, e.g: <script> var nums = "<?php echo json_encode(array(10,20)); ?>"; nums = JSON.parse(nums); </script><script src="nums.js"></script> Commented Feb 11, 2014 at 23:51
  • Tried it that way, still my array in nums.js is not overwritten. Commented Feb 12, 2014 at 0:09
  • You can't overwrite a variable in a different file like that... You'll need to pass the variable into the script using a function of some sort. Commented Feb 12, 2014 at 0:35
0

As long as your javascript file is being parsed by PHP before being sent to the client, then this should work:

<?php echo "var nums=[" . implode(',', $nums) . "];"; ?>
7
  • +1, simple, good ol' PHP. Though I heard using single quotes gives faster page load ;). Commented Feb 11, 2014 at 23:27
  • What do you mean by parsed? I run my javascript at the end of the page like this: <script src="nums.js"></script> Commented Feb 11, 2014 at 23:30
  • @DanielLisik Logically, that's true, but I can't imagine it ever making a noticeable difference. Still, I actually usually use single quotes for text strings anyway. Commented Feb 11, 2014 at 23:31
  • @user3179249 - then you should be fine. As long as it's included in the same file as your PHP. Commented Feb 11, 2014 at 23:31
  • 1
    NO. Don't build the array representation manually. This is poor coding practice. Use json_encode(). Among other things this will make sure values are properly escaped and data types are properly sent. Commented Feb 11, 2014 at 23:37
0

Just json_encode() the array and then output it as an array literal in javascript.

<?php
$nums = array(10,25,52,32,35,23);
$nums_json = json_encode($nums);
?>
<script type="text/javascript">
var nums = <?php echo $nums_json; ?>;
</script>
0

you can also use json_encode() but only possible from php 5.2 see more: https://www.php.net/manual/fr/function.json-encode.php

or you can use this function:

function php2js ($var) {
    if (is_array($var)) {
        $res = "[";
        $array = array();
        foreach ($var as $a_var) {
            $array[] = php2js($a_var);
        }
        return "[" . join(",", $array) . "]";
    }
    elseif (is_bool($var)) {
        return $var ? "true" : "false";
    }
    elseif (is_int($var) || is_integer($var) || is_double($var) || is_float($var)) {
        return $var;
    }
    elseif (is_string($var)) {
        return "\"" . addslashes(stripslashes($var)) . "\"";
    }
    return FALSE;
}
1
  • This FIRST suggestion should be to use json_encode(), not to use it as an afterthought. Even if you don't have native PHP json_encode() there are PHP libraries out there to do this for you. You should not be manually building the JSON representation from the array. Commented Feb 11, 2014 at 23:36

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.