1

I'm having troubles with determining if someone is in an array or not:

<?php
    $depts = $db->query("SELECT depts FROM tbl_staff WHERE id = '".$_SESSION['exp_user']['userid']."'");
    $department = $depts->fetch_assoc();

    if (!in_array($forumdn['permissions'],$department)) {
           error_404("Permissions Violation");
           return;
    }

?>

The value of $forumdn['permissions'] given by var_dump is: string(1) "0"

The value of $department given by var_dump is:

array(1) { ["depts"]=> string(13) "0,1,2,3,4,5,6" }

Any ideas?

3
  • 1
    uargs. strings as list of foreign keys? it might be better to restructure the db and bring it into NF. the query could then be easily made on an sql level without the need to perform the operation in php Commented Jun 18, 2011 at 19:31
  • Trying to restructure this would be a complete nightmare - everything would need to be rewritten :/ Commented Jun 18, 2011 at 19:35
  • Shamil: that's why you create an API to access the database. You only have to fix the API internals and the clients accessing it. If you don't structure your code and it is a big spagetti mess you are in trouble :) Commented Jun 18, 2011 at 20:14

3 Answers 3

2

Try

$department = explode(',', reset($depts->fetch_assoc()));

without any other changes.

Does this do what you need? (I can't be certain because the question isn't worded very clearly to my eyes).

As I read it, the only problem is that $department is indeed an array, but not the array you want. So you have to isolate the first (and only) value in that array with reset (which gives the string "0,1,2,3,4,5,6") and then make that into the array 0 => 0, 1 => 1 etc (using explode) and then finally call in_array.

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

2 Comments

Downvoter: Please help me improve this answer by providing some feedback. Thanks.
Hi Jon - was trying to hit the checkbox.
2

According to the results of your var_dump call, it looks like your results is an array with one string in it - all of the departments (comma separated). My suspicion is that you wanted to have the results end up being separated by commas as individual array elements (so, element 1 contains the value "13", elements 2 contains the value "14", etc). You can use explode for this:

$depts = $db->query("SELECT depts FROM tbl_staff WHERE id = '".$_SESSION['exp_user']['userid']."'");
$department = $depts->fetch_assoc();
$department = explode(",", $department);

Then your call to in_array should work dandy!

Comments

1

try :

if (!in_array($forumdn['permissions'], explode("," $department["depts"]))
{
    ....
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.