【问题标题】:merge two array value in laravel在laravel中合并两个数组值
【发布时间】:2017-02-22 13:29:42
【问题描述】:

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

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

从查询中得到

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

我想与返回的数组 $type1 合并

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 中;

$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

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    如果你想接收合并的array(而不是collection),你可以使用toArray()array_merge()函数:

    $mergedArray = array_merge($type0->toArray(), $type1->toArray());
    

    【讨论】:

    • 在 7BB347C8C194579700A647DADD62EA3D8E0AB68A.PHP 行 358 中捕获 FATALERROREXCEPTION:在阵列上调用成员函数 TOARRAY()
    【解决方案2】:

    您似乎对每个值都有一个集合。因此,您可能是独立获取每个值,而不是一次性获取。

    您的问题可能通过使用whereIn 而不是多个wheres 来解决。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2021-11-21
      • 2018-07-31
      • 2019-12-03
      • 2010-09-08
      • 2010-12-31
      • 2013-08-26
      相关资源
      最近更新 更多