How to move array from PHP to JS ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to move array from PHP to JS ?

We have $whole_arr associative array in php, and...we need it to move to wholeArr in js. We can use them both on same page. Any ideas?(exept saving it in invisible element and getting , that's not cool :D) (only using js ,php,html,css if needed.)

22nd Apr 2017, 8:19 PM
Rose Sevenyears
Rose  Sevenyears - avatar
3 Answers
+ 2
<?php $sts = array( "one"=>"one text", "two" => "two text", "three" => "text three" ) ?> <script> var t= <?php echo json_encode($sts);?> console.log(t) </script> works perfect👍
22nd Apr 2017, 9:17 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 20
echo "<script>arr='".implode(",",$array)."'.split(',');</script>"; (...not tested...)
22nd Apr 2017, 8:08 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
@Yaroslav Pieskov That's it :) ___ Допер в чем косяк...возвращал в переменную в php накой то... вместо вывода...
22nd Apr 2017, 9:27 PM
Rose Sevenyears
Rose  Sevenyears - avatar