F
F
FANTASANTA2020-04-06 18:33:26
PHP
FANTASANTA, 2020-04-06 18:33:26

How to select id in turn?

Greetings!

There are several userids, let's say they look like this:

$users = [1,2,3,4,5];
$lastuser = 1;


The task is as follows: I take one id from the $users array and send it some data, then I write this id to $lastuser. Now the question is - how can I select the next id from the array, taking into account $lastuser?

Let's say if $lastuser=3 - then accordingly $users should return 4 to me - and so on. (from 5 to 1).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-04-06
@FANTASANTA

In general, the literal answer

$next = array_search($lastuser,$users)+1;
$user = $users[$next] ?? $users[0];

But I would choose directly from the database
select manager from users where id > ? order by id limit 1

and if the first one does not find anyone
select manager from users order by id limit 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question