array_merge():参数 #1 不是 laravel 5.5 中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50016058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:40:22  来源:igfitidea点击:

array_merge(): Argument #1 is not an array in laravel 5.5

phparrayslaravel

提问by shahzad1122

I am facing some issue and this about array_merge in laravel My Controller code method:

我在laravel我的控制器代码方法中遇到了一些关于array_merge的问题:

    public function isConnectedMA()
{
    $user_id = Auth::user()->id;
    if(!empty($user_id)) {
        $isConnectedM = DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get();
        $isConnectedA = DB::table('user_mlc_aweber')->where('user_id', $user_id)->get();
    }
        $MergeArray = array_merge($isConnectedM,$isConnectedA);
    $resultArray = ['status' => 1, 'message' => 'Template uploaded!', 'dataArray' => $MergeArray];
    return Response::json($resultArray,200);

}

the error i am facing is

我面临的错误是

array_merge(): Argument #1 is not an array

array_merge():参数 #1 不是数组

i dont know where i am wrong any help will be highly appreciated !

我不知道我错在哪里,任何帮助将不胜感激!

回答by Sohel0415

Use toArray()to convert collectionsto array

使用toArray()转换collections为数组

$isConnectedM = DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get()->toArray();
$isConnectedA = DB::table('user_mlc_aweber')->where('user_id', $user_id)->get()->toArray();

回答by dvl333

In laravel ->get();returns the collection object, you should user ->toArray()here.

在 laravel->get();返回集合对象,你应该->toArray()在这里使用。

回答by fumi

$isConnectedM and $isConnectedA is Collection object. Response::json($collection,200);automatic collection object to json conversion

$isConnectedM 和 $isConnectedA 是 Collection 对象。 Response::json($collection,200);自动收集对象到json转换

$MergeArray = $isConnectedM->merge($isConnectedA) ;
$resultArray = ['status' => 1, 'message' => 'Template uploaded!', 'dataArray' => $MergeArray];
return Response::json($resultArray,200);

回答by bharadwaja Gummadi

@shahzad, Reason for the issue for array_merge we've to pass two arrays, if we didn't pass then it will throw an error.

@shahzad,array_merge 问题的原因我们必须传递两个数组,如果我们没有传递,那么它会抛出一个错误。

 public function isConnectedMA() {
    $user_id = Auth::user()->id;
    // If user id is not passed, return or redirect to login page.
    if (empty($user_id) {
        // TODO : Do logic to retun
        $resultArray = ['status' => 0, 'message' => 'OOPS! seems like user is not logged in.', 'dataArray' => []];
        return Response::json($resultArray, 401);
    }

    $isConnectedM = DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get()->toArray();
    $isConnectedA = DB::table('user_mlc_aweber')->where('user_id', $user_id)->get()->toArray();
    $MergeArray = array_merge($isConnectedM,$isConnectedA); // array_merge((array)$isConnectedM, (array)$isConnectedA);
    $resultArray = ['status' => 1, 'message' => 'Template uploaded!', 'dataArray' => $MergeArray];
    return Response::json($resultArray,200);
}

Referencearry_merge - http://php.net/manual/en/function.array-merge.php

参考arry_merge - http://php.net/manual/en/function.array-merge.php

回答by Andrei Todorut

$isConnectedMand $isConnectedAare not arrays. You can also use typecastto convert an objectto array.

$isConnectedM并且$isConnectedA不是arrays。您还可以使用typecast将 an 转换objectarray.

$isConnectedM = (array) DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get();
$isConnectedA = (array) DB::table('user_mlc_aweber')->where('user_id', $user_id)->get();