如何在php中排序对象数组

时间:2017-03-20 08:49:46

标签: php mysql

我有一个如下所示的数组,

[{
   "name":"Daniel",
   "connection_status":"1"
   },
 {
   "name":"Danny",
   "connection_status":"3"
   },
   {
   "name":"Moris",
   "connection_status":"2"
   },
   {
   "name":"Manny",
   "connection_status":"1"
   }]

我想按顺序按1,2,3这样的状态对数组进行排序。 这是我的代码,

 public function getProfileDataForMySociety($user_id) 
    {
        $this->db->select('*');
        $this->db->from('profile');
        $this->db->where('profile_id!=', $user_id);
        $query = $this->db->get();
        $list = $query->result();
        $friends = $this->checkFriends($list, $user_id);
        return $friends;
    }

  public function checkFriends($list, $user_id) 
{
    $array = [];
    foreach ($list as $k => $v) {
        //     print_r(json_encode($list));
        $friends = $this->checkStatus($v->profile_id);
        //print_r($friends);
        $relationship = '';
        $relation_id = '';
        foreach ($friends as $kk => $vv) {
            if ($user_id == $vv->sent_id) {

                if ($vv->status == 1) {
                    $relationship = 1;
                }

                if ($vv->status == 2) {
                    $relationship = 2;
                }
            } else if ($user_id == $vv->recieved_id) {
                // pending
                if ($vv->status == 1) {
                    $relationship = 3;
                    $relation_id = $vv->sent_id;
                }

                if ($vv->status == 2) {
                    $relationship = 4;
                }
            }
        }
        $list[$k]->connection_status = $relationship;
        $list[$k]->relation_id = $relation_id;
    }
    return $list;
}

public function checkStatus($id) 
{
    $this->db->select('*');
    $this->db->from('requests');
    $this->db->where('sent_id', $id);
    $this->db->or_where('recieved_id', $id);
    $query = $this->db->get();
    $list = $query->result();
    return $list;
}

连接状态不是数据库字段。 $ list是我的o / p数组。任何人都可以帮助我。谢谢。 我想根据我的connection_status对数组进行排序。

3 个答案:

答案 0 :(得分:0)

如果我理解正确,这可能会对你有所帮助

public function getProfileDataForMySociety($user_id) 
{
        $this->db->select('*');
        $this->db->from('profile');
        $this->db->where('profile_id!=', $user_id);
        $this->db->order_by('status', 'asc');
        $query = $this->db->get();
        $list = $query->result();
        return $list;
}

答案 1 :(得分:0)

您可以在查询中应用订单

    public function getProfileDataForMySociety($user_id) 
    {
        $this->db->select('*');
        $this->db->from('profile');
        $this->db->where('profile_id!=', $user_id);
        $this->db->order_by('status');  // USE ORDER BY
        $query = $this->db->get();
        $list = $query->result();
        return $list;
    }

答案 2 :(得分:0)

public function getProfileDataForMySociety($user_id) 
{
    $this->db->select('*');
    $this->db->from('profile');
    $this->db->where('profile_id!=', $user_id);
    $query = $this->db->order('status asc')->get();
    $list = $query->result();
    return $list;
}