代码之家  ›  专栏  ›  技术社区  ›  Josh

如何循环遍历foreach直到找到X,如果找不到,则查找Y

  •  0
  • Josh  · 技术社区  · 6 年前

    抱歉,如果这是重复的,我不知道要搜索什么才能找到答案。

    我有一个foreach循环,在这个循环中,我试图测试(a==B)。一旦发现,我就打破了这个循环。如果(A!=B)在迭代中,我测试If(X==Y)。

    我的问题是,如果首先发现(X==Y)为真,那么在测试if(A==B)之前,循环就会中断。

    有没有更好的方法来完成这项任务?

    $variable[1] = ['A' => 'n', 'X' => 'n'];
    $variable[2] = ['A' => 'n', 'X' => 'Y'];
    $variable[3] = ['A' => 'B', 'X' => 'n'];
    
    $test = 'B';
    
    foreach ($variable as $value) {
    
        if($value['A'] == $test || $value['X'] == "Y") {
            echo 'The results: ' . $value['A'];
            break;
        }
    
    }
    
    // The results for $variable[2] are returned. I need the results for $variable[3] to be returned.
    

    我确实有一个else语句工作得很好,但我不得不复制输出。

    提前谢谢!

    上面的代码是我正在研究的简化版本。这是我正在实际工作的代码。

    foreach ($product_xml->products->product_styles as $style => $attribute) {
        if(isset($_GET['color']) && $attribute['color'] == $color_selected || $attribute['is_default'] == "1") {
            foreach ($attribute as $value){
                $imgURL = (string)$value['imgurl'];
                $thumburl = (string)$value['thumburl'];
                $thumburl_array[(string)$value['side']] = (string)$value['thumburl'];
                if (in_array($imgURL, $values)){continue;}
                else{
                    array_push($values, $imgURL);
                    $imgURL = str_replace("REPLACE_DOMAIN_WITH",IDEQ_INKSOFTAPI_URL_SECURE,$imgURL );
                    $thumburl = str_replace("REPLACE_DOMAIN_WITH",IDEQ_INKSOFTAPI_URL_SECURE,$thumburl );
                    $thumburl = str_replace("150.png","500.png",$thumburl );
                    echo '<img src="'.$imgURL.'" class="pic'.$counter.'" title="'.$value['name'].'">';
                    $counter++;
                }
            }
            break;
        }
    }
    
    4 回复  |  直到 6 年前
        1
  •  2
  •   Isaac Hildebrandt    6 年前

    使用临时变量并移动 echo 至之后 foreach .

    $variable[1] = ['A' => 'n', 'X' => 'n'];
    $variable[2] = ['A' => 'n', 'X' => 'Y'];
    $variable[3] = ['A' => 'B', 'X' => 'n'];
    
    $test = 'B';
    $output = null;
    
    foreach ($variable as $value) {
    
        if($value['A'] == $test) {
            $output = $value['A'];
            break;
        } else if ($output == null && $value['X'] == "Y") {
            $output = $value['X'];
        }
    }
    
    echo 'The results: ' . $output;
    
        2
  •  0
  •   user1860638    6 年前

    下面是我对一个函数的近似:

    function search($variable, $test){
        $alternative = null;
    
        foreach($variable as $value){
            if($value['A'] == $test){
                return $value['A'];
            }
            if($value['X'] == 'Y' && $alternative === null){
                $alternative = $value['A'];
            }
        }
    
        return $alternative;
    }
    

    它将返回A上的第一个重合,如果找不到,则返回X上的第一个重合。

    这样你只能循环通过 foreach公司 一旦

        3
  •  0
  •   Andreas    6 年前

    可以使用array_column来隔离数组A或X中的一列,而不是循环。
    然后使用in_数组查看是否在数组中找到$test。

    $test = 'B';
    $test2 = 'Y';
    
    If(in_array($test, array_column($variable, "A"))){
        Echo $test . " Found in A";
    }Else if(in_array($test2, array_column($variable, "X"))){
        Echo $test2 . " Found in B";
    }else{
        Echo "none found";
    }
    

    https://3v4l.org/dv7Yp

        4
  •  -1
  •   Josh    6 年前

    根据来自的响应 James Lalor 我找到了这个解决方案。如果有人能听到更好的处理方式,我会更喜欢的!

    $counter = 1;
    $values = array();
    $thumburl_array = array();
    $found = false;
    $find_default = false;
    $style_count = count($product_xml->products->product_styles);
    
    for ($i=0; $i < $style_count; $i++) { 
        if (isset($_GET['color']) && $product_xml->products->product_styles[$i]['color'] == $color_selected) {
            $found = true;
        } elseif (!$found && !$find_default && $i == $style_count - 1) {
            $find_default = true;
            $i = 0;
        }
    
        if ($find_default && $product_xml->products->product_styles[$i]['is_default'] == '1') {
            $found = true;
        }
    
        if ($found) {
            foreach ($product_xml->products->product_styles[$i] as $value){
                $imgURL = (string)$value['imgurl'];
                $thumburl = (string)$value['thumburl'];
                $thumburl_array[(string)$value['side']] = (string)$value['thumburl'];
                if (in_array($imgURL, $values)){continue;}
                else{
                    array_push($values, $imgURL);
                    $imgURL = str_replace("REPLACE_DOMAIN_WITH",IDEQ_INKSOFTAPI_URL_SECURE,$imgURL );
                    $thumburl = str_replace("REPLACE_DOMAIN_WITH",IDEQ_INKSOFTAPI_URL_SECURE,$thumburl );
                    $thumburl = str_replace("150.png","500.png",$thumburl );
                    echo '<img src="'.$imgURL.'" class="pic'.$counter.'" title="'.$value['name'].'">';
                    $counter++;
                }
            }
            break;
        }
    }
    

    简化版

    $variable[] = ['A' => 'n', 'X' => 'n', 'result' => 'do'];
    $variable[] = ['A' => 'n', 'X' => 'Y', 'result' => 're'];
    $variable[] = ['A' => 'B', 'X' => 'n', 'result' => 'mi'];
    
    $found = false;
    $find_default = false;
    $count = count($variable);
    
    $test = 'B';
    
    for ($i=0; $i < $count; $i++) { 
        if ($variable[$i]['A'] == $test) {
            $found = true;
        } elseif (!$found && !$find_default && $i == $count - 1) {
            $find_default = true;
            $i = 0;
            continue;
        }
    
        if ($find_default && $variable[$i]['X'] == 'Y') {
            $found = true;
        }
    
        if ($found) {
            echo "Resulsts: " . $variable[$i]['result'];
            break;
        }
    }
    

    谢谢大家的反馈,非常感谢。干杯