I have a very simple PHP array
我有一个非常简单的PHP数组
$array = [];
$array['a'] = '1';
$array['b'] = '2';
$array['c'] = '3';
PHP
If I dd($array);
out I got
如果我dd($ array);我得到了
array:3 [▼
"a" => "1"
"b" => "2"
"c" => "3"
]
If I decode dd(json_encode($array));
, I got this
如果我解码dd(json_encode($ array));,我得到了这个
"{"a":"1","b":"2","c":"3"}"
JS
I want to be able to access this variable in my Javascript, So I've tried
我希望能够在我的Javascript中访问此变量,所以我试过了
1
console.log($array);
I got
$array is not defined
$ array未定义
2
I'm using Laravel. {{ }} == echo
我正在使用Laravel。 {{}} == echo
console.log('{{$array}}');
I got
500 Internal Error
500内部错误
htmlentities() expects parameter 1 to be string, array given (View: /Users/bheng/Sites/portal/resources/views/cpe/index.blade.php)
htmlentities()期望参数1为字符串,给定数组(查看:/Users/bheng/Sites/portal/resources/views/cpe/index.blade.php)
3
console.log('{{ json_encode($array)}}');
I got
The page to load, but the data is very bad looking
要加载的页面,但数据看起来很糟糕
{"a":"1","b":"2","c":"3"}
4
console.log(JSON.parse('{{ json_encode($array)}}'));
I got
Uncaught SyntaxError: Unexpected token & in JSON at position 1
未捕获的SyntaxError:位于1的JSON中出现意外的令牌
5
console.log(JSON.parse('{{ json_decode($array)}}'));
I got
json_decode() expects parameter 1 to be string, array given
json_decode()期望参数1为字符串,给定数组
6
console.log('{{ json_decode($array)}}');
I got
json_decode() expects parameter 1 to be string, array given
json_decode()期望参数1为字符串,给定数组
GOAL
I just want to be able to access my array as Javascript Array or JSON in the Javascript.
我只是希望能够在Javascript中以Javascript Array或JSON的形式访问我的数组。
Can someone please fill me in on this ?
有人可以请我填一下吗?
4 个解决方案
#1
6
In Blade, {{ $variable }}
will output an escaped version of the string, passed through htmlentities()
to make it safe for use in HTML. You want an unescaped version. You can use {!! $variable !!}
for that:
在Blade中,{{$ variable}}将输出字符串的转义版本,通过htmlentities()传递,以便在HTML中使用。你想要一个未转义的版本。您可以使用 {!! $ variable !!}为此:
console.log({!! json_encode($array) !!});
You don't need to add quotes around it, json_encode()
outputs a valid javascript object. It will add quotes where necessary, if you add them yourself you will get the JSON string in your javascript, instead of the JSON object.
你不需要在它周围添加引号,json_encode()输出一个有效的javascript对象。它会在必要时添加引号,如果你自己添加它们,你将获得javascript中的JSON字符串,而不是JSON对象。
#2
1
In Laravel you can use {!! !!} to skip entity escaping
在Laravel你可以使用{!! !!}跳过实体转义
console.log({!! json_encode($array) !!});
#3
0
Just echo it as json data and use it in javascript.
只需将其作为json数据回显并在javascript中使用它。
<?php
$array = [];
$array['a'] = '1';
$array['b'] = '2';
$array['c'] = '3';
?>
<script>var jsArr = <?=json_encode($array);?>;
alert(jsArr);</script>
EDIT because of clarification that you're using blade. Then it should be:
编辑,因为澄清你正在使用刀片。那应该是:
<?php
$array = [];
$array['a'] = '1';
$array['b'] = '2';
$array['c'] = '3';
?>
<script>var jsArr = {!! json_encode($array) !!};
alert(jsArr);</script>
{ ... } is an escaped version of your string. But you need the unescapt string. This can be achieved by using {!! ... !!}.
{...}是字符串的转义版本。但是你需要unescapt字符串。这可以通过使用{!! ...... !!}。
#4
0
First, you have to understand that PHP run on server side and javascript on client side, as PHP make the response you should print a script like this:
首先,您必须了解PHP在服务器端运行并在客户端运行javascript,因为PHP会做出响应,您应该打印如下脚本:
echo "<script>
var sheison = JSON.parse(".dd(json_encode($array)).");
console.log(sheison);
</script>";
I didn't test the code, is just the idea.
我没有测试代码,只是想法。
#1
6
In Blade, {{ $variable }}
will output an escaped version of the string, passed through htmlentities()
to make it safe for use in HTML. You want an unescaped version. You can use {!! $variable !!}
for that:
在Blade中,{{$ variable}}将输出字符串的转义版本,通过htmlentities()传递,以便在HTML中使用。你想要一个未转义的版本。您可以使用 {!! $ variable !!}为此:
console.log({!! json_encode($array) !!});
You don't need to add quotes around it, json_encode()
outputs a valid javascript object. It will add quotes where necessary, if you add them yourself you will get the JSON string in your javascript, instead of the JSON object.
你不需要在它周围添加引号,json_encode()输出一个有效的javascript对象。它会在必要时添加引号,如果你自己添加它们,你将获得javascript中的JSON字符串,而不是JSON对象。
#2
1
In Laravel you can use {!! !!} to skip entity escaping
在Laravel你可以使用{!! !!}跳过实体转义
console.log({!! json_encode($array) !!});
#3
0
Just echo it as json data and use it in javascript.
只需将其作为json数据回显并在javascript中使用它。
<?php
$array = [];
$array['a'] = '1';
$array['b'] = '2';
$array['c'] = '3';
?>
<script>var jsArr = <?=json_encode($array);?>;
alert(jsArr);</script>
EDIT because of clarification that you're using blade. Then it should be:
编辑,因为澄清你正在使用刀片。那应该是:
<?php
$array = [];
$array['a'] = '1';
$array['b'] = '2';
$array['c'] = '3';
?>
<script>var jsArr = {!! json_encode($array) !!};
alert(jsArr);</script>
{ ... } is an escaped version of your string. But you need the unescapt string. This can be achieved by using {!! ... !!}.
{...}是字符串的转义版本。但是你需要unescapt字符串。这可以通过使用{!! ...... !!}。
#4
0
First, you have to understand that PHP run on server side and javascript on client side, as PHP make the response you should print a script like this:
首先,您必须了解PHP在服务器端运行并在客户端运行javascript,因为PHP会做出响应,您应该打印如下脚本:
echo "<script>
var sheison = JSON.parse(".dd(json_encode($array)).");
console.log(sheison);
</script>";
I didn't test the code, is just the idea.
我没有测试代码,只是想法。