You were on the right path!
Either use (this shows the SQL more clearly):
$query = $this->db->query('SELECT max(shiftId) shiftId FROM yourtable GROUP BY endTime')->result_array();
Or (if you want to use CI's query builder):
$query = $this->db->select_max('shiftId')->group_by('endTime')->get('yourtable')->result_array();
Both of these group the table by endTime, and then return the maximum shiftId for each group of identical endTimes. Both give an array that looks like this:
Array
(
[0] => Array
(
[shiftId] => 2
)
[1] => Array
(
[shiftId] => 4
)
)
To get rid of the shiftId index in the result and get the exact array structure from your OP, use:
array_column($query, 'shiftId');
Edit
If you want to get the shiftId for each endTime + MAX(shift) combination, use this:
SELECT shiftId FROM yourtable
WHERE CONCAT(endTime, "-", shift) IN (
SELECT CONCAT(endTime, "-", MAX(shift)) FROM yourtable GROUP BY endTime
)
The inner query (after IN) does more or less the same as the previous query: it groups the records in the table by endTime, then gets the maximum shift for each group of identical endTimes, and here it concatenates this with the endTime and a dash.
You need to concatenate endTime with MAX(shift) here, because MAX(shift) alone is not unique in the table (there's more than one shift with number 2, for example), and neither is endTime.
The outer query (SELECT shiftId...) then finds the matching shiftId for each endTime + MAX(shift) combination and returns that.
You need to use two (nested) queries for this, because the inner one uses grouping and the outer one doesn't, and you're not allowed to mix those two types in one query.
Note: CONCAT only works in MySQL, if you're using a different database type, you might have to look up what concatenation syntax it uses (could be + or || for example).
In CI:
$query = $this->db->query('SELECT shiftId FROM yourtable
WHERE CONCAT(endTime, "-", shift) IN (SELECT CONCAT(endTime, "-", MAX(shift)) FROM yourtable GROUP BY endTime)')->result_array();