Craft Element API数据输出为哈希而不是数组

时间:2018-03-28 10:31:33

标签: php json api serialization

我正在使用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密钥。

有没有办法摆脱这个数组级别?

0 个答案:

没有答案