如何在mailchimp中只获取字段(列)

时间:2018-03-05 05:38:14

标签: php laravel mailchimp

我正在使用值获取字段名称,但我只想在mailchimp中获取字段名称:

我的控制器代码:

 public function getFieldsListMailchimp(Request $request)
{
    $list_id = $request->input('list_id');
    $user_id = $request->user()->id;
    $apikey = DB::table('user_mlc_mailchimp')->where('user_id', '=', $user_id)->value('api_key');
    if ($list_id) {
        $mc = new MailChimp($apikey);
        $fields = $mc->get('/lists/'.$list_id.'/members?fields=members.id,members.email_address,members.merge_fields');
        $fields = ['status' => 1, 'message' => 'Fields list!', 'dataArray' => $fields];
        return Response::json($fields, 200);

    } else {
        $errorResponse = [
            'message' => 'Lists not found!',
            'error' => '401'
        ];
        return Response::json($errorResponse);
    }
}

我得到的代码是:

  {

 "status": 1,
"message": "Fields list!",
"dataArray": {
    "members": [
        {
            "id": "663657d6bee76fdd3285c879bf2849e2",
            "email_address": "shahzad.hussain@vaivaltech.com",
            "merge_fields": {
                "FNAME": "shahzad",
                "LNAME": "Hussain"
            }
        },
        {
            "id": "b59a741183b7e1990156a46faa29b60c",
            "email_address": "Hello@gmail.com",
            "merge_fields": {
                "FNAME": "Shahzad",
                "LNAME": "Hussain"
            }
        }
    ]
}

正如你可以看到我想只获得字段名称(id,email_address,merger_fields(FNAME,LNAME),但在这里我得到字段名称以及值我只想在这里获取字段名称? 非常感谢您的帮助! 提前谢谢。

        public function getFieldsListMailchimp(Request $request)
{
    $list_id = $request->input('list_id');
    $user_id = $request->user()->id;
    $apikey = DB::table('user_mlc_mailchimp')->where('user_id', '=', $user_id)->value('api_key');
    if ($list_id) {
        $mc = new MailChimp($apikey);
        $fields = $mc->get('/lists/'.$list_id.'/members?fields=members.id,members.email_address,members.merge_fields');
        $fields = ['status' => 1, 'message' => 'Fields list!', 'dataArray' => array_keys($fields)];
        return Response::json($fields, 200);

    } else {
        $errorResponse = [
            'message' => 'Lists not found!',
            'error' => '401'
        ];
        return Response::json($errorResponse);
    }
}

1 个答案:

答案 0 :(得分:0)

 $fields = [
    'status' => 1, 
    'message' => 'Fields list!', 
    'dataArray' => array_keys($fields)
 ]);
 return Response::json($fields, 200);

Array Keys,只会为您提供密钥