gpt4 book ai didi

PHP : Laravel - Array push after using eloquent pluck method

转载 作者:可可西里 更新时间:2023-11-01 00:19:02 28 4
gpt4 key购买 nike

这是我的查询

$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..
)

)

最佳答案

这是因为 $RecipientList 是集合而不是数组。

试试这个

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();

希望对您有所帮助。

关于PHP : Laravel - Array push after using eloquent pluck method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40234528/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com