Php:Laravel - 使用雄辩的拔除方法后的数组推送

时间:2016-10-25 08:02:51

标签: php arrays laravel array-push

这是我的查询

$RecipientList = Employees::select(DB::Raw('CONCAT(first_name," ",last_name) as employee_name'),'email')->pluck('employee_name','email');

给我正确的结果,

但在我执行查询后,我还有1个键=>值对推入结果数组。

如果我打印当前结果,就像这样。

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [punit@*****.com] => Punit Gajjar
            [milan@*****.com] => Milan Gajjar
            [pritesh@*****.com] => Pritesh Modi
            [pratik@*****.com] => Pratik Modi
            [jyoti@*****.com] => Jyotiranjan J..
        )

)

如果我尝试将我的Key => valye对推入此数组,那么它是否有效。

array_push(array("All"=>"All"),$RecipientList);

需要输出类似

的内容
Illuminate\Support\Collection Object
    (
        [items:protected] => Array
            (
                [All] => All
                [milan@*****.com] => Milan Gajjar
                [milan@*****.com] => Milan Gajjar
                [pritesh@*****.com] => Pritesh Modi
                [pratik@*****.com] => Pratik Modi
                [jyoti@*****.com] => Jyotiranjan J..
            )

    )

2 个答案:

答案 0 :(得分:5)

这是因为$ RecipientList是Collection而不是Array。

试试这个

RecipientList = Employees::select(DB::Raw('CONCAT(first_name," ",last_name) as employee_name'),'email')->pluck('employee_name','email')->toArray();

如果这不起作用,请尝试以下代码

RecipientList = Employees::select(DB::Raw('CONCAT(first_name," ",last_name) as employee_name'),'email')->get()->pluck('employee_name','email')->toArray();

希望这会对你有所帮助。

答案 1 :(得分:2)

你有Illuminate\Support\Collection个对象不是数组。你可以做到

$RecipientList->push(["All"=>"All"]);

UPD:有prepend方法

$RecipientList->prepend('All', 'All');