【问题标题】:How to loop through a foreach until X is found, if not found, look for Y如何循环遍历一个foreach直到找到X,如果没有找到,寻找Y
【发布时间】:2018-05-16 15:19:42
【问题描述】:

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

我有一个 foreach 循环,在那个循环中,我试图测试 if (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;
    }
}

【问题讨论】:

  • 使用 for 循环,在最后一个循环中将 $i 重置为 0,切换标志,然后进行 Y 检查
  • 感谢您的建议,詹姆斯。我已根据您的回复提交了答案。该解决方案 100% 有效。我想做的唯一改进是不必循环两次来执行多个测试。

标签: php arrays if-statement search foreach


【解决方案1】:

使用临时变量并将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;

【讨论】:

  • 也许你的意思是在结尾回显$output
  • 感谢您的解决方案。一旦找到,我必须执行一些额外的功能,而不是只是一个回声。我简化了我的问题中的代码,希望它更容易分析。我将添加我现在正在实际处理的代码以供参考。
【解决方案2】:

这是我对函数的近似:

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】:

    您可以使用 array_column 来隔离数组 A 或 X 的一列,而不是循环。
    然后使用 in_array 查看是否在数组中找到 $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

    【讨论】:

    • 我不能有三个独立的回声。回声只是占位符。实际上,我不想重复一些额外的功能。
    • @Josh 这就是你的问题。如果您不喜欢这样,那么您不应该在您的问题中发布它。如果您想掩盖您的真实代码或数据,那么稍后实现它是您的问题。如果你给了我们真正的代码,你就会得到一个可以直接使用的答案。
    • 感谢您的反馈。我希望简化它会使分析更容易。
    【解决方案4】:

    根据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;
        }
    }
    

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

    【讨论】:

    • 看我的回答,这比任何循环都好得多
    • 我现在会调查一下并回复你。
    • 只是澄清一下,詹姆斯在 cmets 中建议的您使用的方法是所有想法中最糟糕的。这意味着您将整个数组循环两次。您在此处获得的其他两个循环答案将数组循环一次。我的答案根本没有循环。
    • 是的,我对循环两次不太兴奋。我正在尝试弄清楚如何实施您的建议。我很近,但不完全在那里。
    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多