1

I have a webapplication with a php backend. At a certain interval checks need to be run on all users. Running these checks takes some time, more importantly: they should not be executed on non-existing users. The users are received from a database and can change mid-way through running the checks. My current solution is:

<?php 
require_once 'databaseUtils.php';

$users = getUsersFromDatabase();
//Sample:
while(true) {
  foreach(GetUsers() as &$user) {
    //I'd rather not:
    if(checkIfUserIsInDatabase($user) {
       var_dump($user);
       sleep(1); //Checking takes time...
    }
  }
  echo "Going again in 5s!";
  sleep(5); //Interval
}

function GetUsers() {
  return $users;
}

//Called from outside
function removeUser($user) {
  global $users;
  removeUserFromDatabase($user);
  if(($key = array_search($user, $users)) !== false) {
      unset($users[$key]);
  }
}
?>

But I'm still looping needlessly through the user that isn't realy in the GetUsers() anymore. Is there a way to loop through all values of an array in which elements can get deleted from outside?

2
  • 1
    Why while(true)? Commented May 2, 2017 at 8:52
  • @SougataBose The code is a crude example. I was just testing something out that needed to be done repetatively. Ultimately this would be set up as a deamon. Commented May 3, 2017 at 8:09

2 Answers 2

1

If you want to check each user if he is still in the database, you will need to query his information from database, so it will take more efforts than checking each item in array (if it is not more resource spending).

But I will suggest to get users in smaller portions instead of all users. And check portion by portion.

Sign up to request clarification or add additional context in comments.

Comments

1

Firstly, you might need to consider to rework your entire business logic.

Do not use endless loops (while(true)) and global variables (global $users).

To loop through all users you can use generator concept and retrieve existent users one by one. So you will get only the next user from the database during each iteration:

function getUsersFromDatabase($db)
{
    $id = 0;

    while (($user = $db->query("SELECT * FROM `users` WHERE `id` > $id LIMIT 1"))) {
        yield $user;
        $id = $user->id;
    }
}

foreach (getUsersFromDatabase($db) as $user) {
    checkUser($user);
}

I do not know your framework or DB adapter details, so the code snippet is generic and should be considered as pseudo code.

1 Comment

@sevacietl I do not need to reconsider my entire business logic. This script was just me testing around a bit. I don't see why I shouldn't use global variables. In this application they've been quite useful to me. With that said, I do think your answer is the way to go for this instance. I'd better loop through the database one by one and make each user in there. That way I don't even have to store all users in memory. The question still begs, is there a way to loop through a dynamic array in php?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.