Which of these arrays is more preferable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Which of these arrays is more preferable?

Post by MichaelR »

Hi. I'm looking to store simple data in arrays. Access levels, to be specific. But I'm not sure which of these is the better method (if one can even be said to be better than the other).

The first is:

Code: Select all

 
$admin = array(
 
 "Michael" => array(
 
  1 => true,
  2 => true,
  3 => true,
  4 => true,
  5 => true,
  6 => true
 
 ),
 
 "Andrew" => array(
 
  1 => true,
  2 => true,
  3 => true,
  4 => true,
  5 => true,
  6 => false
 
 )
 
);
 
The second is:

Code: Select all

 
$admin = array();
 
 $user = "Michael";
 
  $admin[$user][1] = true;
  $admin[$user][2] = true;
  $admin[$user][3] = true;
  $admin[$user][4] = true;
  $admin[$user][5] = true;
  $admin[$user][6] = true;
 
 $user = "Andrew";
 
  $admin[$user][1] = true;
  $admin[$user][2] = true;
  $admin[$user][3] = true;
  $admin[$user][4] = true;
  $admin[$user][5] = true;
  $admin[$user][6] = false;
 
Thank you in advance.
Last edited by MichaelR on Sat Jan 03, 2009 7:05 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Which of these arrays is more preferable?

Post by requinix »

So you do know they're both the same then?

The first would be nicer if you hard-coded these access levels directly into a file and the second would probably be easier if you get the levels from another source (like a database).
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Which of these arrays is more preferable?

Post by MichaelR »

Yes, I do know they're the same. I just wondered if one was more acceptable than another. Like with "if (...) ... else (...) ...". You can write them in a number of ways, but some are considered 'better coding' than others.

I plan on having them hard-coded into a file. So I'll take your advice and go with the first.

Thanks.
Post Reply