我正在使用Element API plugin from Craft构建和使用API,我希望将数据输出序列化为哈希值,但目前它正在返回一个数组,如下所示:
<?php
namespace Craft;
return [
'endpoints' => [
'event-name/feed.json' => [
'elementType' => ElementType::Entry,
'criteria' => ['section' => 'event1'],
'transformer' => function(EntryModel $entry) {
$speakersList = [];
foreach ($entry->speakersList as $speaker) {
$speakersList[] = [
'name' => $speaker->speakerFullName,
'jobTitle' => $speaker->speakerJobTitle,
'company' => $speaker->speakerCompany,
];
}
return [
'speakers' => $speakersList,
];
},
],
];
输出:
{
"data": [
{
"speakers": [
{
"name": "John Doe",
"jobTitle": "Marketing",
"company": "Company 1",
},
...
我在文档中尝试过序列化选项,但似乎都没有解决问题。
目前,如果我想访问speakers
中的data
,我必须先访问index[0]
才能访问speakers
密钥。
有没有办法摆脱这个数组级别?