0

I am trying to get a group tabel to work on my website. I have three tables in my db. One is for users. Second is a list of different group names. The last one is a table with all the details that each group has an access too. The last table have a parentGroup that matches the groupListID and have a row called access. It can be any kind of number, per example 1-100.

Then I am trying to get a if statement if any of the access numbers matches number one, in this example:

        $this->db->where('parentGroup', $groups);
        $this->db->select('access');
        $query = $this->db->get('groupDetail');

        $array = $query->result_array();


        $key = array_search(1, array_column($array, 'access'));

        if (array_search(1, array_column($array, 'access'))) {
            echo 'This array does contain the number one';
        }
        else
        {
            echo 'This array does not contain the number one';

            //redirect('dashboard');
        }

The problem is that it does not print out the value of access, but the value of the array that have the value of access and that cannot be used in my if statement...

I hope that this makes sense? Any help is really appreciated!

  • Are you using a framework like Laravel, CakePHP, Zend, etc? If so, please add the associated tag to the question. – Patrick Q Jan 22 '20 at 13:33
  • I am using codeigniter... The plan is that i am taking the if statement out of the model and putting it into the controller... But the main thing is that it would work :-) – jonasalbertsen Jan 22 '20 at 13:35
  • "The problem is that it does not print out the value of access, but the value of the array that have the value of access and that cannot be used in my if statement..." Can you try to reformulate? I don't get it all – Wimanicesir Jan 22 '20 at 13:35
  • Array ( [0] => Array ( [access] => 1 ) ) 0 – jonasalbertsen Jan 22 '20 at 13:36
  • You should probably be doing this check as a query, but the answer to this actual problem is explained in the documentation for [`array_search()`](https://www.php.net/manual/en/function.array-search.php): "Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE." – Patrick Q Jan 22 '20 at 13:37
  • the last zero is the id if the array that have the data of acces that is 1. The only thing I want is the data from the access... – jonasalbertsen Jan 22 '20 at 13:37
  • But can you help me on the way how to do this... right now I am pretty lost. I just have it in my head but getting it down to paper is an another thing... – jonasalbertsen Jan 22 '20 at 13:43
  • Did you go read the documentation? Specifically the box from which I quoted? Do you understand what it means when it says "may also return a non-Boolean value which evaluates to FALSE"? Did you read the bit that comes after that which explains how to handle those cases? – Patrick Q Jan 22 '20 at 13:47
  • I have read that document. And this code I am using is an inspiration a comment from xfoxawy at gmail dot com. But as the comments below his says that it does not output the value of 40489 in his example, but the value of 2 because of that the array that contain 40489 is the array number two. But in my case it is not usable because all that I want the the 40489 if we go with his example... – jonasalbertsen Jan 22 '20 at 13:51
  • The does really help my out. What I did was this. And I think it really works for me! if (array_search(1, array_column($array, 'access')) !== FALSE) Thank you very much! – jonasalbertsen Jan 22 '20 at 14:04

0 Answers0