Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a java script array from php array? [duplicate]

Iam using PHP,Smarty configuration.

Iam getting an array in PHP and sent it to tpl.I would like to make a javascript array in tpl from that php array.

PHP ARRAY

Array
(
    [0] => Array
        (
            [dt] => 2011-12-02
            [number] => 3
        )

    [1] => Array
        (
            [dt] => 2011-12-05
            [number] => 3
        )

    [2] => Array
        (
            [dt] => 2011-12-07
            [number] => 2
        )

)

and I want to get it as a java script array in tpl

s1 = [[[2011-12-02, 3],[2011-12-05,3],[2011-12-07,2]]]; 
like image 550
user1029108 Avatar asked Dec 07 '11 10:12

user1029108


People also ask

How send data from array to JavaScript in PHP?

Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation(JSON). Method 1: Using json_encode() function: The json_encode() function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.

How do I copy an array in JavaScript?

Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the reference to the original array and not the value of the array. To create a real copy of an array, you need to copy over the value of the array under a new value variable.

How can I get unique values from two arrays in PHP?

The array_diff() (manual) function can be used to find the difference between two arrays: $array1 = array(10, 20, 40, 80); $array2 = array(10, 20, 100, 200); $diff = array_diff($array1, $array2); // $diff = array(40, 80, 100, 200);

What is Array_map function in PHP?

The array_map() is an inbuilt function in PHP and it helps to modify all elements one or more arrays according to some user-defined condition in an easy manner. It basically, sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that function.


3 Answers

You'd have to loop through it to generate that array

<script>
   var arr = [];
   <?php foreach ($arr as $item) : ?>
   arr.push(['<?php echo $item['dt']?>', <?php echo $item['number']?>]);
   <?php endforeach; ?>
</script>

This will give you an array of arrays.

However, I would do it in another way. I'd just encode it with json and use it as an object

<script>
   var data = <?php echo json_encode($arr)?>;
   for (var x in data) {
      //data[x].dt;
      //data[x].number;
   }
</script>
like image 64
JohnP Avatar answered Sep 23 '22 23:09

JohnP


I really dislike the idea of inline PHP all over the place. We don't inline JavaScript or CSS these days because it's bad practice i.e. difficult to maintain. We appreciate separation of concerns. If you need to pass data from your application (PHP or otherwise) to JavaScript variables on the client side then there are two good mechanisms you can use. Either get the result as JSON via AJAX, or simply render the variables on the client side in a single accessible location.

Your templating engine should really be able to register the data on the page for you. What you are essentially doing is rendering a script, so your script include mechanism should take an optional argument for registering php variables. If you are not using a templating engine and only need to inject the variables, I guess it would be acceptable to do something along the lines of.

<script type="text/javascript">
    var phpdata = <?php echo json_encode($phpdata)?>
</script>

Note that this is declaring a global. We are encouraged to avoid the usage of globals at all costs but I think it's a valid use case when sensibly chosen, such as if you register your own object in global scope as a sensible namespace, then refer to namespace.phpdata. Alternatively you simply wrap your JavaScript in an immediate function call and declare the variable in local scope.

function () {
    var phpdata = <?php echo json_encode($phpdata)?>

    ...
}();

But like I said, in my opinion I think it's better to get your templating engine to handle it, so you pass the PHP data you need for each javascript include, and a single php data object is generated from this data and included in your page.

like image 36
Matt Esch Avatar answered Sep 22 '22 23:09

Matt Esch


You can use the php function json_encode to encode the array to json and then use it as an array like object in javascript

like image 36
Nenad Avatar answered Sep 23 '22 23:09

Nenad