0

I'm trying to build a routine which outputs nav menu depending on user status. It needs to be fed a nested array variable (company => role) which looks like:

array(2) {
  ["Company 1"]=>
  array(2) {
    [0]=>
    string(3) "dir"
    [1]=>
    string(5) "manag"
  }
  ["company 2"]=>
  string(3) "dir"
}

It is assumed, that a user can have multiple roles.

Now my routine (simplified version, just to show the logic isnt working):

function get_menu_1  ($status) {

        foreach ($status as $company => $position) {
            $a = is_array($company); //THIS ALWAYS RETURNS FALSE this is for debugging
            echo "<br>this element is array = $a<br>"; //this is for debugging
            if (true == is_array($company)) { // THIS ALWAYS RETURNS FALSE this user in this company has multiple roles

                foreach ($company as $subcompany => $subposition) {
                    echo "<br>$subposition<br>";
                }
            } else { //its not an array, user has one role in the company

                echo "<br>$position<br>";
            }

        }

    }

The output is:

Notice: Array to string conversion in /sata2/home/users/xreact/www/cert.xreact.org/functions.php on line 393

Array

this element is array = 

dir
array(2) { ["Company 1"]=> array(2) { [0]=> string(3) "dir" [1]=> string(5) "manag" } ["Company 2"]=> string(3) "dir" }

For some reason is_array() fails to check whether the variable is array.

1
  • You may try to add the positions as array no matter if there is only one position. Commented Oct 17, 2016 at 12:01

1 Answer 1

5

you are testing if your array-key is an array itself - but it can't be.

you have to test the value instead.

foreach ($status as $company => $position) {
    echo is_array($company); //will allways be false, because the array key is a string.
    //in your examples, "Company 1" or "Company 2";
    if(is_array($position)) {
        echo "here you have your nested array";
    }
}

edit

also, on a site node: you could circumvent the check alltogether if you improve your data structure a little bit. instead of storing a single role as string, you can store it as an array with one string element - so your array-value allways is an array:

$data = array(
    "Company 1"=>array("dir", "manag"),
    "Company 2"=>array("dir")
);
Sign up to request clarification or add additional context in comments.

2 Comments

Fielen Dank, but it doesn't work. The check itself returns false and on the inner foreach i get a warning thrown: Invalid argument supplied for foreach() As for your suggestion of changing data structure, this is definitely a good and more effective way, however right now its quite a bit tricky to implement.
Got it, it had to supply position to foreach as well! Thank you!

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.