如何使用foreach循环替换此处的位置?当我把foreach放在数组中时,代码就会中断。我猜测答案与创建一个foreach变量然后输入水平值相反?
这是我的数组
$data = array(
'name' => Locations,
'data' => '{
"title":"USA",
"location":"World",
"levels":[
{
"id":"states",
"title":"States",
"locations":[
{
"id":"ca",
"title":"California",
"pin":"hidden",
"x":"0.0718",
"y":"0.4546",
},
{
"id":"wa",
"title":"Washington",
"pin":"hidden",
"x":"0.1331",
"y":"0.0971"
}
]
}
]
}'
这是我目前的预告。
foreach ($response->records as $record) {
$id = $record->Id;
$title = $record->Title;
$pin = $record->Pin;
$x = $record->X;
$y = $record->Y;
echo {
"id": $id,
"title": $title,
"pin": $pin,
"x": $x,
"y": $y
}
}
任何帮助都将不胜感激。
答案 0 :(得分:0)
$arr = array();
foreach ($response->records as $record) {
$r['id'] = $record->Id;
$r['title'] = $record->Title;
$r['pin'] = $record->Pin;
$r['x'] = $record->X;
$r['y'] = $record->Y;
$arr[] = $r;
}
echo json_encode($arr);