标签:dir json stc encode block array data str extend
<?php
$text = ‘文本‘;
$articles = [
[
‘title‘ => ‘标题1‘,
‘content‘ => ‘内容1‘
],
[
‘title‘ => ‘标题2‘,
‘content‘ => ‘内容2‘
]
];
?>
<script>
//字符串
var text = ‘<?php echo $text ?>‘;
console.dir(text);
//数组
var articlesJson = ‘<?php echo json_encode($articles); ?>‘;
var articles = JSON.parse(articlesJson);
console.dir(articles);
</script>
php
class TestController extends Controller
{
function test()
{
$data = [];
$data[‘text‘] = ‘文本‘;
$data[‘articles‘] = [
[
‘title‘ => ‘标题1‘,
‘content‘ => ‘内容1‘
],
[
‘title‘ => ‘标题2‘,
‘content‘ => ‘内容2‘
]
];
return view(‘test/test‘,$data);
}
}
js
<script>
//字符串
var text = ‘{{$text}}‘;
console.dir(text);
//数组
var articlesJson = ‘{!! json_encode($articles) !!}‘;
var articles = JSON.parse(articlesJson);
console.dir(articles);
</script>
控制台输出结果
文本
Array(2)
0: {title: "标题1", content: "内容1"}
1: {title: "标题2", content: "内容2"}
length: 2
标签:dir json stc encode block array data str extend
原文地址:https://www.cnblogs.com/mg007/p/12755292.html