Page 1 of 1

Which of these arrays is more preferable?

Posted: Sat Jan 03, 2009 3:33 pm
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.

Re: Which of these arrays is more preferable?

Posted: Sat Jan 03, 2009 6:06 pm
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).

Re: Which of these arrays is more preferable?

Posted: Sat Jan 03, 2009 6:16 pm
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.