I have a very simple PHP array
$array = []; $array['a'] = '1'; $array['b'] = '2'; $array['c'] = '3';
Php
If I dd($array); came out i got
array:3 [โผ "a" => "1" "b" => "2" "c" => "3" ]
If I decode dd(json_encode($array)); , I got it
"{"a":"1","b":"2","c":"3"}"
Js
I want to have access to this variable in my Javascript, so I tried
1
console.log ($ array);
I got
$ array not defined
2
I am using Laravel. {{ }} == echo
console.log ('{{$}} array');
I got
500 Internal Error
htmlentities () expects parameter 1 to be the string given by the array (View: /Users/bheng/Sites/portal/resources/views/cpe/index.blade.php)
3
console.log ('{{json_encode ($ array)}}');
I got
Page to load, but the data looks very bad
{"a":"1","b":"2","c":"3"}
4
console.log (JSON.parse ('{{json_encode ($ array)}}'));
I got
Untrained SyntaxError: Unexpected token in JSON at position 1
5
console.log (JSON.parse ('{{json_decode ($ array)}}'));
I got
json_decode () expects parameter 1 to be the string given by the array
6
console.log ('{{json_decode ($ array)}}');
I got
json_decode () expects parameter 1 to be the string given by the array
TASK
I just want to have access to my array as a Javascript Array or JSON in Javascript.
Can someone please fill me in?