首页 > 解决方案 > 试图在选择中获取邮件诗人列表

问题描述

我正在尝试使用下面的代码在下拉列表中获取邮件诗人列表,但我只获得选项中的第一个值和 id,尽管有 5 个列表可用,我做错了什么?

    $mplistname = array(\MailPoet\API\API::MP('v1')->getLists()[0]['name']);
    $mplistid = array(\MailPoet\API\API::MP('v1')->getLists()[0]['id']);
    $mplistoptions = array_combine($mplistid, $mplistname);

    foreach($mplistoptions as $key=>$value) {
            $mplistoptions[$key] = $value;
    }

标签: phparrays

解决方案


正如 Patryk 所说,当一个就足够时,不要进行两次 API 调用。

你只得到名字和id,因为你只访问两个数组中的第一个条目……就是[0]这样。

这可能应该被浓缩成这样的东西:

$data = \MailPoet\API\API::MP('v1')->getLists();

foreach($data as $record) {
    // do stuff with $record['name'] and $record['id'] here
}

推荐阅读