在laravel中合并两个数组值 [英] merge two array value in laravel

查看:195
本文介绍了在laravel中合并两个数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在laravel 5.3中合并两个数组,我有一个变量'$ type'返回

I want to merge two array in laravel 5.3.I have variable '$type' which return

`Illuminate\Support\Collection Object ( [items:protected] => Array ( [1] => rinu )`

从查询中获取

$type0=DB::table('users')->where('name','=',$head)->pluck('name','id');

我想与返回$

Illuminate\Support\Collection Object ( [items:protected] => Array ( [2] => john ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [3] => dinesh ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( [4] => mahesh ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( ) )

$type1=DB::table('users')->where('name','=',$head)->pluck('name','id');

我试图合并并将其存储在$ type0;

I tried to merge and store it in $type0;

$type0=$type0->merge($type1);

返回错误值

Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh [3] => mahesh ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh [3] => mahesh ) )`

enter code here

推荐答案

您似乎每个值都有一个Collection.因此,您可能是独立获取而不是一次性获取每个值.

You seem to have a Collection for each value. So you are probably fetching each value independently instead of in one go.

您的问题可能是通过使用 whereIn 而不是许多哪里 s.

Your problem is probably solved by using whereIn instead of a number of wheres.

$result = DB::table('users')
            ->whereIn('name', ['John', 'Dinesh', 'Mahesh'])
            ->get();

这篇关于在laravel中合并两个数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆