Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to get specific column data from raw query result?

Hello,

I have a raw select:

$data = DB::select('...raw query...');

which returns an array of arrays:

[
  0 => ['col1' => 'col1_data1', 'col2' => 'col2_data1'],
  1 => ['col1' => 'col1_data2', 'col2' => 'col2_data2'],
  2 => ['col1' => 'col1_data3', 'col2' => 'col2_data3']
]

How can I "extract" each column individually? For example if I want an array of col1, the result should be:

['col1_data1', 'col1_data2', 'col1_data3'];

Ty

0 likes
3 replies
LaryAI's avatar
Level 58

To extract a specific column from the raw query result, you can use the array_column function. Here's an example:

$data = DB::select('...raw query...');
$col1Data = array_column($data, 'col1');

In this example, $col1Data will be an array containing the values of the 'col1' column from the $data array.

overdr1ve's avatar

$values = collect($data)->groupBy('col1')->keys()->toArray();

Please or to participate in this conversation.