【问题标题】:identifying Identical array key matches and specifying output for different matching sets识别相同的数组键匹配并为不同的匹配集指定输出
【发布时间】:2013-05-12 20:02:33
【问题描述】:

我创建了一个简单的函数,它根据哪些键具有匹配的值返回一个值,但如果键为空或 null 则不计算在内。我的代码如下。

function getFontData(){

    $font = [];
    $font['Font'] = 'Arial';
    $font['Font2'] = 'Arial';
    $font['Font3'] = 'Arial';

    return $font;

}


function compareFonts(){

    $compare = 0;


    $font = getFontData();



   if (!empty($font['Font']) && $font['Font'] === $font['Font2']) {
        $compare = 1;
    } elseif (!empty($font['Font']) && $font['Font'] === $font['Font3']) {
        $compare = 2;
    } elseif (!empty($font['Font2']) && $font['Font2'] === $font['Font3']) {
        $compare = 3;

    }elseif (!empty($font['Font']) && $font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
        $compare = 4;
    };

    return $compare;

}

$matches = compareFonts();

var_dump($matches);

我遇到的问题是,我为识别三个相同数组键的函数编写的代码似乎没有在我的 if 语句中执行。此示例中的输出为 1。这表明我的代码正在返回第一个 if 语句,这在本质上是正确的,因为 'font' 和 'font2' 都匹配,因此代码不需要更进一步。

我假设有更好的方法可以做到这一点,或者我的方法完全错误。欢迎所有建议。

问候

w9914420

【问题讨论】:

    标签: php arrays if-statement multidimensional-array string-matching


    【解决方案1】:

    elsif 有问题:

    if (!empty($font['Font']) && $font['Font'] === $font['Font2']) {
            $compare = 1;
        } elseif (!empty($font['Font']) && $font['Font'] === $font['Font3']) {
            $compare = 2;
        } elseif (!empty($font['Font2']) && $font['Font2'] === $font['Font3']) {
            $compare = 3;
    
        }elseif (!empty($font['Font']) && $font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
            $compare = 4;
        };
    

    在此代码中,第一个if 将评估为TRUE,因此不会测试任何elsif,代码将直接跳转到末尾。你需要这样的东西:

    $compare=0;
    if (!empty($font['Font']){ 
        if($font['Font'] === $font['Font2']) {
            $compare++;
        }
        if ($font['Font'] === $font['Font3']) {
            $compare++;
        }
        if ($font['Font2'] === $font['Font3']) {
            $compare++;
        }
        if ($font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
            $compare++;
        }
    }
    

    撇开这个问题不谈,这段代码有点太具体了,如果你现在需要多 10 种字体怎么办?也许我们可以试着让它更简单一点:

    $font = [];
    for($x=0; $x<10;$x++){  //10 fonts
        $font['Font'.$x] = 'Arial';
    }
    
    $compare=0;
    if (!empty($font['Font']){ 
        for($x=0; $x<count($font); $x++){
            for($y=1;$y<(count($font)-$x+1);$y++){
                 if($font['Font'.$x] == $font['Font'.$y]){
                     $compare++;
                 }
            }
        }
    }
    

    我没有太多时间来测试这个,但它应该比之前的代码更容易维护。

    【讨论】:

    • 你好 Naryl,我只想说谢谢你让我深入了解这个问题。我的代码需要指定执行条件,因此必须严格。请随时评论我的回答。 - w9914420
    【解决方案2】:

    正如 Naryl 指出的那样,嵌套 If 语句是前进的方向。

    $compare=0;
    if (!empty($font['Font']){ 
    if($font['Font'] === $font['Font2']) {
        $compare++;
    }
    if ($font['Font'] === $font['Font3']) {
        $compare++;
    }
    if ($font['Font2'] === $font['Font3']) {
        $compare++;
    }
    if ($font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
        $compare++;
    }
    }
    

    此代码将是完美的,除了 if 语句将仅在 font1 的条件与 font 3 相同时执行第一个语句。因此我必须创建一个更明确的搜索条件并想出这个。

        function compareFonts(){
    
        $compare = 0;
        $font = getFontData();
    
    if (!empty($font['Font'])){ 
    
        if ($font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
        $compare = 1;
        }
    
        if($font['Font'] === $font['Font2'] && $font['Font3'] == NULL) {
        $compare = 2;
        }
        }
    if (!empty($font['Font2'])){
        if ($font['Font2'] === $font['Font3'] && $font['Font'] == NULL) {
        $compare = 3;
        }
    }
    
    if (!empty($font['Font3'])) {
    
        if ($font['Font'] === $font['Font3'] && $font['Font2'] == NULL) {
        $compare = 4;
        }
    
    }
    
        return $compare;
    
    } 
    

    我对这个问题的想法是,由于条件是基于所有三种字体的值,那么 if 语句参数解决这个问题是有道理的。

    // this will execute regardless of the value of [font'3]
    $font['Font'] === $font['Font2']
    
    // this is very specific in that [font'3] must be null.
    $font['Font'] === $font['Font2'] && $font['Font3'] == NULL
    
    // This works because empty array key is converted to null by non-strict equal 
    // '==' comparison. For example the following code shows this.
    
    $cho = [];
    $cho['key'] = '';
    
    if ($cho['key'] == Null){
        echo 'true';
    }
     // returns true.
    

    同样,这是一个非常独特的条件,但可能有更好的方法来做到这一点,所以请随时发表评论。

    再次感谢 w9914420。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 2022-11-19
      • 1970-01-01
      • 2016-06-12
      相关资源
      最近更新 更多