nick
nick

Reputation: 891

How to get mysql select join result as 1 dimension array?

I am using this select join query to get associated doctor ids only knowing the id for table b. The results look like this:

Array ( [0] => Array ( [id] => 2 ) [1] => Array ( [id] => 40 ) )

What I want is like this, the same but without the extra array [id]:

Array ( [0] => 2 [1] => 40  )

Here is the query:

  select a.doctor_id as id from assigned_doctors a INNER JOIN appointments b
  ON a.appointment_number = b.appointment_number WHERE b.id = 283

Is there some way I can change the query to get that result? I guess this format is caused by using the join. Not a big deal, but would rather not have to deal with the indexes.

Upvotes: 1

Views: 217

Answers (2)

harsh kumar
harsh kumar

Reputation: 165

    We can use like this to get result :

    while($row = $result->fetch_array()) {
        $ids[$row['id']] = $row['id'];
    }

    array_keys($ids);
OR array_values($ids);

Upvotes: 2

AbraCadaver
AbraCadaver

Reputation: 78994

Change your fetch to only store the id and not the entire row array. You haven't shown it, but regardless of the API it is similar:

while($row = $result->fetch_assoc()) {
    $ids[] = $row['id'];
}

If you've gotten this through a fetchAll() or just for future reference, you can just extract the id column from the array of row arrays:

$ids = array_column($rows, 'id');

Upvotes: 3

Related Questions