比较数组键值以确定条件语句的OOP方式?
I have been looking at this problem for quite sometime and finally created my answer.
I have three array keys that will output different values. The condition of each key will either be true or NULL and this is what I am testing for my array is simple and looks like this.
$a = array();
$a['Font'] = logix::templateParams('googleWebFont');
$a['Font2'] = logix::templateParams('googleWebFont2');
$a['Font3'] = logix::templateParams('googleWebFont3');
I wanted to create output dependent on the collective condition of all three keys for example if one key is null then the output would be different then If I had 2 keys 'NULL'. I managed to create a simple switch which covered the range of values that I needed to cover my code looks like this.
$a = array();
$a['Font'] = logix::templateParams('googleWebFont');
$a['Font2'] = logix::templateParams('googleWebFont2');
$a['Font3'] = logix::templateParams('googleWebFont3');
switch (TRUE){
// No font selected
case $a['Font'] == NULL && $a['Font2'] == NULL && $a['Font3'] == NULL:
echo 'nothing';
break;
// First googlefont selected only
case $a['Font'] && $a['Font2'] == NULL && $a['Font3'] == NULL:
echo 'one';
break;
// Second googlefont selected only
case $a['Font2'] && $a['Font'] == NULL && $a['Font3'] == NULL:
echo 'two';
break;
// Third googlefont selected only
case $a['Font3'] && $a['Font2'] == NULL && $a['Font'] == NULL:
echo 'three';
break;
// and Continues to cover another 10 more states.......
So far this works fine and covers each possible variation that I need to cover. I was wondering if there was a more flexible way to do this. For example if I wanted to add another array value and compare the condition of the collective then this solution is not flexible enough to do so. I would have to completely re-write the case of the switch although this is unavoidable is there a more efficient way of doing this. Im completely new to PHP but I have been read a little on OOP and I just wondered what would be the OOP way of doing this. To be more clear of what I am trying to achieve.
// 1. collect the array keys
// 2. evaluate keys and check for certain conditions
// 3. output based on conditions
Is there a more flexible way of doing this?
regards
w9914420
Use a multi-dimensional table, where the dimensions correspond to the fonts, and the indexes correspond to whether that font is set:
$font_table = array(array(array('nothing', 'one'),
array('two', 'three')),
array(array('four', 'five'),
array('six', 'seven')));
echo $font_table[!empty($a['Font3'])][!empty($a['Font2'])][!empty($a['Font'])];
What you are trying to do is find all binary combinations of 3 cases. Now you have 3 keys, which can be true or null. So you have 2x2x2 = 8 combinations.
First step will be to evaluate each case with its number. For example FFT = 1, FTT=3, TTT = 7 etc.
For example case $a['Font'] == NULL && $a['Font2'] == NULL && $a['Font3'] == NULL: = FFF since in all case the value is NULL, so value evaluates to 000 = 0 decimal.
$a['Font'] && $a['Font2'] == NULL && $a['Font3'] == NULL: = TFF, which evaluates to 100=4 decimal.
The trick here is that each Font has different weight. Font3 is 1, Font2 is 2, Font3 is 4, Font4(for future) is 8.
After you calculate this values, you can match each case in your switch:
case '0':
echo 'nothing';
break;
// First googlefont selected only
case '1':
echo 'one';
break;
If then you have 4 keys, you can still evaluate as FFFT=1, FFTF = 2 etc. The switches will stay the same, and only need to add the additional cases.