返回多维数组的第一行

时间:2022-05-26 00:55:32

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

出于某种原因,我想只显示每个image_id的第一行或第一行图像。例如,参考array [image_id] => 1有2个不同的图像,[image_id] => 2也有2个不同的图像,但我想返回[image_id] => 1只有1个图像而[image_id] => 2只有1张图片

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

1 个解决方案

#1


3  

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

如果您使用的是mysql数据库,请添加Group By语句。

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

#1


3  

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

如果您使用的是mysql数据库,请添加Group By语句。

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