How do I use a variable in an array name in PHP? (variable arrays/variable variable?) -


i've looked @ other questions/answers i'm not understanding it. furthermore, haven't found 1 asking exact question.

i have 7 arrays storing start , end times business. php program checks current day , matches start end time day. right have 7 if statements similar this:

if (date(l) == 'sunday') {    $start = $sundayaccountinghours['start'];    $end = $sundayaccountinghours['end'];  } 

what want clean code. maybe have

$start = ${$today}.accountinghours['start']; $end = ${$today}.accountinghours['end']; 

how can make work? using above example i'm getting this: parse error: syntax error, unexpected '[' in c:\xampp\htdocs\dev.php on line 22 line 22 $start defined. can't take out stuff in brackets because that's information need to.

if can't tell, i'm still novice @ php appreciated.

thanks!

if (date(l) == 'sunday') {    $start = $sundayaccountinghours['start'];    $end = $sundayaccountinghours['end'];  } 

first, put quotes around 'l'. it's looking constant named l, , failing find it's treating string. bad practice (and should issue warning).

secondly, use multi-dimensional array. don't need try , use day-of-the-week part of variable name. e.g.,

$start = $accountinghours[$today]['start']; 

you might discover variable-variables down road allow you're trying, advise steer clear of them. don't think there's single practical application them , serve cause confusion , error (cough).


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -