Emerald
Emerald

Reputation: 874

Return first row of multidimensional array

I have this value inside my database which is inserted using multiple upload.

Array
(
    [0] => Array
        (
            [user_id] => 1
            [image_id] => 1
            [title] => testing
            [image_name] => 59435c5f0838f.jpg
        )

    [1] => Array
        (
            [user_id] => 1
            [image_id] => 1
            [title] => testing
            [image_name] => 58435c5f0838f.jpg
        )

    [2] => Array
        (
            [user_id] => 1
            [image_id] => 2
            [title] => testing
            [image_name] => 57435c5f0838f.jpg
        )

    [3] => Array
        (
            [user_id] => 1
            [image_id] => 2
            [title] => testing
            [image_name] => 56435c5f0838f.jpg
        )
)

For some reason I want to display only first or one row of image of each image_id. For example refer to array [image_id] => 1 have 2 different images and [image_id] => 2 also have 2 different images, but I want to return [image_id] => 1 only 1 image and [image_id] => 2 only 1 image

My model function selecting from database

public function loadAll(){
    $this->db->select('*');
    $this->db->from('tbluser');
    $this->db->where('user_id', $this->getUserId());
    $query = $this->db->get();
    $row = $query->result_array();
    if($row != null){
        return $row;
    } else {
        return false;
    }
}

Upvotes: 1

Views: 112

Answers (1)

Asif Rahaman
Asif Rahaman

Reputation: 775

Add a Group By statement if you are using mysql database.

$query = $this->db->select('*')
          ->from('tbluser')
          ->where('user_id', $this->getUserId())
          ->group_by('image_id')  /* Add this group by statement */
          ->get();

Upvotes: 3

Related Questions