要合并的json字段的内容 - Laravel应用程序

时间:2017-05-19 10:35:42

标签: php json laravel-5.4

我需要一个唯一的关键字列表。这是查询

$array['keywords'] = DB::table('job_posts')
                ->pluck('keywords')->toArray();

在我的job_posts表中,关键字列是一个json条目[laravel cast json]

对于记录1,关键字字段值为

["jquery","html","css","js"]

对于记录1,关键字字段值为

["jquery","html","scss"]

我需要一个像这样的数组

array('jquery','html','css','js','scss')

以便为选择框()

提供选项

1 个答案:

答案 0 :(得分:1)

Laravel有一些你可以使用的酷阵列helpers

所以,例如:

$test = [
    ['a', 'b', 'c', 'd'],
    ['b', 'c', 'd', 'e']
];

$flattened = array_flatten($test);
$unique    = array_unique($flattened);

dd($unique);

会输出:

array:5 [▼
  0 => "a"
  1 => "b"
  2 => "c"
  3 => "d"
  7 => "e"
]