内部数组json_en code变量 [英] json_encode variable inside array
问题描述
我使用json_en code创建从我想静态数组里面放置一个foreach的数组。我已经加载了json_en codeD数组变量。如果我附和变量,数据看起来不错。这只是当我输入变量放入数组,这是行不通的。
I am using json_encode to create an array from a foreach that I would like to place inside of a static array. I have loaded the json_encoded array into a variable. If I echo the variable, the data looks great. It's just when I enter the variable into the array, it doesn't work.
这是我的foreach。
This is my foreach.
$arr = array();
foreach ($response->records as $record) {
$r['id'] = $record->Id;
$r['title'] = $record->Title;
$r['pin'] = $record->Pin;
$arr[] = $r;
}
$locations = json_encode($arr);
这是我的,我已经进入了$位置静态数组变量。
And this is my static array where I have entered the $locations variable.
$data = array(
'name' => Locations,
'data' => '{
"title":"USA",
"location":"World",
"levels":[
{
"id":"states",
"title":"States",
"locations":'$locations'
}
]
}'
);
这它,我看到的错误。
Parse error: syntax error, unexpected '$locations' (T_VARIABLE), expecting ')'
我想AP preciate的帮助,谢谢。
I would appreciate the help, thank you.
推荐答案
您正在寻找的小玩意儿是的连接符它看起来像这样
The gizmo you're looking for is the concatenation operator which looks like this
.
据胶合串在一起。
It glues strings together.
$data = array(
'name' => Locations,
'data' => '{
"title":"USA",
"location":"World",
"levels":[
{
"id":"states",
"title":"States",
"locations":' . $locations . '
}
]
}'
);
这篇关于内部数组json_en code变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!