2

I have the following PHP array

$menu['Settings']['user'] = array(

    1=>'General',
    'database.php'=>'Database',
    'users.php'=>'User Management'

);

However the entry for database.php needs to depend on if a string called $user=="Admin"

Any idea how I can put an if statement inside this array?

1
  • Use a ternary statement: 'database.php'=> ($user === 'Admin') ? 'Database' : 'SomeOtherValue', Commented Jan 13, 2014 at 14:49

2 Answers 2

8

Use a ternary operation?

'database.php' => $user === 'Admin' ? 'Database' : 'Grumblecakes',
Sign up to request clarification or add additional context in comments.

Comments

1

Doing this outside of the array is much easier for beginners than a ternary operation.

$menu['Settings']['user'] = array(
    1=>'General',
    'database.php'=>'Database',
    'users.php'=>'User Management'
);

if($user == "Admin") {
    $menu['Settings']['user']['database.php'] = "";
}

2 Comments

What's hard about a ternary operation? What does it have to do with being a beginner, even?
It can be confusing for some people. I guess it'd be worth it for them to learn now, but some people I know actually prefer this method over ternary.

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.