I have a user table which I've encrypted and stored in a MySQL database.
When pulling the data back out and adding it to a form via a select input control I'd like to be able to order the items in alphabetical order.
I am decypting the items on each iteration of a foreach loop.
<?php foreach ($data['crewData'] as $crew) : ?>
<option value="<?php echo $inputCrew; ?>">
<?php echo decryptData($crew->user_firstName).' '.decryptData($crew->user_lastName); ?>
</option>
<?php endforeach; ?>
I know I can sort an array using sort($variable);
but the data being returned by $data['crewData']
is a standard object as the snippet below shows...
array(38) {
[0]=>
object(stdClass)#18 (21) {
["user_id"]=>
string(2) "20"
["user_firstName"]=>
string(59) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
I'd like to sort by the user_firstName field. My only thought as to how to do this is run a foreach loop decrypting the data, creating a temporary non-encrypted object then presenting this to the select.
I only have around 50 users so far so the overhead isn't too bad but as the user count increases I can see this being a pain??
To go the above route will this work?
$crewData = $this->userModel->getUserByType('Crew');
$array = [];
foreach ($crewData as $item) {
$firstName = decryptData($item->user_firstName) //decryptData() is my decryption function
array_push($array, $firstName );
}
$arraySorted = sort($array);