我试图生成以下JSON数据:
<div class="row video-section">
@if(!(count($videos) > 0))
<div class="col-md-12">
<p>{{ $player->first_name }} {{ $player->last_name }} has no videos yet.</p>
</div>
@else
@foreach($videos as $video)
<div class="col-md-3 profile-video-box">
<a href="/player/video/{{ $video->uid }}/{{ $video->player->fullName }}">
<div class="card bg-dark text-white">
<img src="{{ $video->getThumbnail() }}" class="card-img"/>
<div class="card-img-overlay">
<div class="play-icon-wrapper">
<i class="ion-ios-play"></i>
</div>
<div class="card-content">
<h5 class="card-title">{{ $video->title != '' ? $video->title : 'Untitled' }}
</h5>
<p class="card-text">{{ $video->created_at->diffForHumans() }}
</p>
</div>
</div>
</div>
</a>
</div>
@endforeach
</div>
来自PHP [
{
"ID": "A1000",
"name": "John Simpson",
"serialID": "S41234",
"tagID": "ABC6456"
},
{
"ID": "B2000",
"name": "Sally Smith",
"serialID": "B5134536",
"tagID": "KJJ451345"
},
{
"ID": "A9854",
"name": "Henry Jones",
"serialID": "KY4239582309",
"tagID": "HYG452435"
}
]
循环,如下所示:
foreach
我尝试过使用:
foreach($records as $record) {
$ID = $record->getField('propertyID');
$name = $record->getField('assignedName');
$serialID = $record->getField('serial');
$tagID = $record->getField('tag');
}
适用于循环中的每条记录,但不能解决语法将它们连接成一个JSON数据字符串吗?
答案 0 :(得分:5)
问题: - 您在$arr
循环内每次覆盖foreach()
变量。导致问题。
解决方案: - 而不是覆盖,请将值分配给数组,如下所示: -
$arr = []; //create empty array
foreach($records as $record) {
$arr[] = array(
'ID' => $record->getField('propertyID');
'name' => $record->getField('assignedName');
'serialID' => $record->getField('serial');
'tagID' => $record->getField('tag')
);//assign each sub-array to the newly created array
}
echo json_encode($arr);
答案 1 :(得分:0)
将记录添加到某种聚合数组,然后json_encode
:
$items = [];
foreach($records as $record) {
$items[] = [
'ID' => $record->getField('propertyID'),
'name' = $record->getField('assignedName'),
'serialID' => $record->getField('serial'),
'tagID' => $record->getField('tag'),
];
}
echo json_encode($items);
答案 2 :(得分:0)
试试这个:
'rules' => [
[
'actions' => ['index', 'view'],
'allow' => true,
'roles' => [RbacComponent::VIEW_EXPENSES_ACCOUNTS_KEY],
],
[
'actions' => ['create'],
'allow' => true,
'roles' => [RbacComponent::CREATE_EXPENSES_ACCOUNTS_KEY],
],
[
'actions' => ['update'],
'allow' => true,
'roles' => [RbacComponent::EDIT_EXPENSES_ACCOUNTS_KEY],
],
[
'actions' => ['delete'],
'allow' => true,
'roles' => [RbacComponent::DELETE_EXPENSES_ACCOUNTS_KEY],
],
[
'actions' => ['index', 'view', 'create', 'update', 'delete'],
'allow' => true,
'matchCallback' => function ($rule, $action) {
return \Yii::$app->user->identity->isOwner();
},
],
],
说明:创建一个数组,将所有值放在其中一个数字索引上,并在循环中$data= array();
foreach($records as $record) {
$data[] = array(
'ID' => $record->getField('propertyID');
'name' => $record->getField('assignedName');
'serialID' => $record->getField('serial');
'tagID' => $record->getField('tag')
);
}
echo json_encode($data);
。
答案 3 :(得分:0)
您可以使用类似array_map()的内容:
$result = array_map(function ($record) {
return [
'ID' => $record->getField('propertyID'),
'name' => $record->getField('assignedName'),
'serialID' => $record->getField('serial'),
'tagID' => $record->getField('tag'),
];
}, $records);
echo json_encode($result);
答案 4 :(得分:0)
<?php
$ArrayName = array();
foreach($records as $record) {
$ID = $record->getField('propertyID');
$name = $record->getField('assignedName');
$serialID = $record->getField('serial');
$tagID = $record->getField('tag');
$ArrayName[] = array('ID' => $ID, 'name' => $name, 'serialID' => $serialID, 'tagID' => $tagID); //assign each sub-array to the newly created array
}
echo json_encode($ArrayName);
?
> Blockquote
答案 5 :(得分:0)
此代码可以帮助您:
<?php
$resultArray = array();
foreach($records as $record)
{
$newResultArrayItem = array();
$newResultArrayItem['ID'] = $record->getField('propertyID');
$newResultArrayItem['name'] = $record->getField('assignedName');
$newResultArrayItem['serialID'] = $record->getField('serial');
$newResultArrayItem['tagID'] = $record->getField('tag');
$resultArray[] = $newResultArrayItem;
}
$encodedArray = json_encode($ArrayName);
echo $encodedArray;
?>