2

I pull a list of permissions from a the DB using and put them into an array;

while($row = mysql_fetch_assoc($get_permissions)) {
    $_SESSION['permissions'][] = $row;  
}    

The contents of the session variable then looks like this;

array(2) { 
    [0]=> array(1) { 
        ["permission_name"] => string(15) "acl_assets_read"
    } 
    [1]=> array(1) { 
        ["permission_name"] => string(16) "acl_assets_write" 
    } 
}

below is the output using print_r instead which makes it easier to read.

Array ( [0] => Array ( [permission_name] => acl_assets_read ) [1] => Array (     [permission_name] => acl_assets_write ) )

I've read about using array_search and think it should work. I've tried to use the following to search for a permission;

if (array_search('acl_assets_read', $_SESSION['permissions'])) {
    echo "true";   
}

The problem i have is that even though the result is there, it keep returning false. The syntax looks correct to me.

Nathan
  • 2,461
  • 4
  • 37
  • 48
  • 2
    `array_search` doesn't search recursive. – Daan Dec 16 '15 at 14:57
  • array_search matches the input to the elements of an array. You are searching for a string in an array that doesn't contain strings, it contains other arrays, which in turn contain strings. So that won't work. – pvg Dec 16 '15 at 14:57
  • Isn't it var_dump that is adding those bits though? – Nathan Dec 16 '15 at 15:01
  • @Nathan can i see how you are adding the permissions to the array... with that i'll give you an answer... edit your question to show it – AceKYD Dec 16 '15 at 15:08

3 Answers3

2

Your problem is that you add the entire row (which is an array of its own) from your database into the $_SESSION['permissions']-array, forming a sub-array for each time it iterates the values from the database.

This means that all values in $_SESSION['permissions'] are arrays, not strings. This in turn means that you cannot search for a string like that.

If you have stored the values you are interested in, in a column named permissions in the database, you simply need to add that element only to your $_SESSION['permissions']-array, like this

$_SESSION['permissions'][] = $row['permissions'];  

This will add the string from that row into an element in the array $_SESSION['permissions'].


It's also worth noting that array_search() returns the key of the array, where as the first element will have an index (key) equal to 0. This means that the very first element of your array would really look like if (0) { /* code */ }. This will return to false (if (0) == false), so you should perhaps look into using ìn_array(), which returns a boolean true/false.

if (in_array('acl_assets_read', $_SESSION['permissions'])) {
    echo "true";   
}

Also, mysql_* functions are deprecated, and you shoud stop using them if you can.

Community
  • 1
  • 1
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Unless you are using PHP 5.4.x or lower. But `This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0.` – Mr. Meeseeks Dec 16 '15 at 15:37
  • It's a statement that you should stop *if you can* - simply because `mysql_` functions are out of date. Most people are on at least PHP 5.5, if you are on a lower version, that alone could be a reasonable argument to change host. But that's not the issue here ;-) – Qirel Dec 16 '15 at 15:40
0

array_search will work for you but you need the proper array to search.

Take a look at this:

$arrOne = array("one", "two");
$arrTwo = array(array("one"), array("two"));

$key = array_search("one", $arrOne);

var_dump($key); // int(0) is the index of where the value was found

$key = array_search("two", $arrTwo);

var_dump($key); // bool(false) because "two" != array("one") or array("two")

Notice that the second array_search will return false because a string does not equal an array.

If you had:

array(2) { 
    [0]=> string(15) "acl_assets_read"
    [1]=> string(16) "acl_assets_write"
}

instead of:

array(2) { 
    [0]=> array(1) { 
        ["permission_name"] => string(15) "acl_assets_read"
    } 
    [1]=> array(1) { 
        ["permission_name"] => string(16) "acl_assets_write" 
    } 
}

then your array_search would find the proper string

Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
0

Here is your answer :

$userdb = array(array("permission_name" => 'acl_assets_read'),array("permission_name" => 'acl_assets_write'));
$key = array_search("acl_assets_write", array_column($userdb, 'permission_name'));

if($key != ''){
  echo 'true';
}

If needle found $key show key of needle. If needle is not found $key show blank.

Monty
  • 1,110
  • 7
  • 15