I have a array that contains other arrays with each user's details in. For example:
$_SESSION["ActQueue"] = [["J.Paul", "John", "Paul", "0000-00-00 00:00:00"],["J.Bloggs", "Joe", "Bloggs", "0000-00-00 00:00:00"]]
I have written a bit of code that is mean to turn the "account rank" of the first user in the array to "Cadet" in the database. The code then removes the user from the array and then should either echo data = 1 or the next user in the list depending if they exist. I have done this with the following bit of code, however I have found that when unset deletes the first item it the keys don't shift down 1, so 0 will simply not exist and 1 will still be "J.Bloggs". As a result when the next user is echoed no values are sent. Any suggestions?
<?php
session_start();
require "classes.php";
$TF = new TF_Core ();
$ActQueueAccept = "UPDATE users
SET rank = 'Cadet'
WHERE username = ?";
if ($statement = TF_Core::$MySQLi->DB->prepare($ActQueueAccept)) {
$statement->bind_param('s',$_SESSION["ActQueue"][0][0]);
$statement->execute();
}
unset($_SESSION["ActQueue"][0]);
if(count($_SESSION["ActQueue"] != 0)){
echo json_encode(['Username'=>$_SESSION["ActQueue"][0][0], 'Surname'=>$_SESSION["ActQueue"][0][1],'Forename'=>$_SESSION["ActQueue"][0][2],'Joined'=>$_SESSION["ActQueue"][0][3]]);
}
else{
$data = 1;
echo $data;
}
?>
unset($_SESSION["ActQueue"][0]);
$_SESSION["ActQueue"] = array_values($_SESSION["ActQueue"]);
It seems that unset()
does not always reset array keys. The function array_values redefines my array with the correct keys / indexes it.