0

I have an array that's made up of data returned by a SQL query:

$_SESSION['licensed_users'] = $db->get_results("SELECT np.id FROM `new_people` np LEFT JOIN institute.users_roles ur ON np.institute_uid = ur.uid WHERE np.company_id=$company_id AND ur.rid=8 AND np.active = 1");

This returns an array that looks like this:

Array ( 
   [0] => stdClass Object ( [id] => 25590 )
   [1] => stdClass Object ( [id] => 40657 )
   [2] => stdClass Object ( [id] => 60685 ) 
   [3] => stdClass Object ( [id] => 61900 )
   [4] => stdClass Object ( [id] => 65224 )
   [5] => stdClass Object ( [id] => 65369 ) 
   [6] => stdClass Object ( [id] => 79171 ) 
   [7] => stdClass Object ( [id] => 80763 )
   [8] => stdClass Object ( [id] => 80762 ) 
   [9] => stdClass Object ( [id] => 80761 )
 )

In another section of the code I loop through the values to see if the current user is part of that array:

foreach($_SESSION'licensed_users'] as $key=>$value) {
   if($value->id == $people_id) {
      $is_licensed = true;
   }
}

$is_licensed is used to determine which set of form fields to display to the user. When the user submits the form, if a certain group of fields is set to a certain value, I need to either add the user ($people_id) to the $_SESSION['licensed_users'] array if $is_licensed is false, or remove them from the array if $is_licensed is true. The code for determining which action to take is no problem; I just can't for the life of me remember/figure out how to add to an array of objects. I've seen this and this, but I already know if the ID is in the array or not; I just need to be able to add or remove it.

(Yes, there is a reason we're using session variables - we need to be able to pass values between pages of the site. It's either that or cookies.)

I've tried $_SESSION['licensed_users'][] = array("id"=>$people_id); but it doesn't seem to actually do anything.

2
  • What about making $db->get_results not only return an array, but index with their id as well. Then you can just do isset($_SESSION'licensed_users'][$people_id]) to determine if such an entry exists (and you will have that one directly if). Commented Nov 21, 2011 at 20:12
  • @hakre - I'm not sure what you mean. How would I force my query to return an array that's indexed by id? Commented Nov 21, 2011 at 20:59

1 Answer 1

2

To remove $lu[6]:

unset($lu[6]);

To add an element:

$lu[] = $current_user;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks; I edited the original post while you were answering, so please take a look. Also, your version of adding wouldn't account for the fact that this is an array of objects - the $people_id value needs to be added as an array with the key of id.
In that case, you'd just make $current_user an object with the data you want. $current_user can be anything. If you're looking for shorthand for converting an array to an object, you can try $current_user = (object)array('id'=>$people_id).
I've tried this: $add_person = (object)array('id'=>$people_id); $_SESSION['licensed_users'][] = $add_person;. It doesn't work; no errors, just doesn't update the array. What am I doing wrong?

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.