dpxyfa4718 2013-05-07 19:35
浏览 50
已采纳

比较数组键值以确定条件语句的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

  • 写回答

2条回答 默认 最新

  • dongxie7683 2013-05-07 19:46
    关注

    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'])];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角
  • ¥15 webots有问题,无响应
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上