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]]];
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.
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.
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);
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.
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>
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With