PHP中的foreach传递数组

PHP中的foreach传递数组,php,arrays,parameters,Php,Arrays,Parameters,我正在尝试使用数组匹配字符串打印 问题是。。。它只打印BTC打印 如果可能,还建议使用数组参数进行简单匹配 代码: <?php $coins = array("BTC", "ETH", "DOGE", "WAVES"); foreach ($coins as $coin) { $string = 'BTC'; if (strpos($string, $coin) !== FALSE) { echo "BTC pri

我正在尝试使用数组匹配字符串打印

问题是。。。它只打印BTC打印

如果可能,还建议使用数组参数进行简单匹配

代码

<?php
    $coins = array("BTC", "ETH", "DOGE", "WAVES");

    foreach ($coins as $coin) {

        $string = 'BTC';
        if (strpos($string, $coin) !== FALSE) {
            echo "BTC print";
            return true;
        }
        $string1 = 'ETH';
        if (strpos($string1, $coin) !== FALSE) {
            echo "ETH print";
            return true;
        }
        $string2 = 'DOGE';
        if (strpos($string2, $coin) !== FALSE) {
            echo "DOGE print";
            return true;
        }
        $string3 = 'WAVES';
        if (strpos($string3, $coin) !== FALSE) {
            echo "WAVES print";
            return true;
        }
    }
    echo "Not found!";
    return false;

返回将中断您的循环。试试这个:

$coins = array("BTC", "ETH", "DOGE", "WAVES");

$found = false;
foreach ($coins as $coin) {
    $string = 'BTC';
    if (strpos($string, $coin) !== FALSE) {
        echo "BTC print";
        $found = true;
    }
    $string1 = 'ETH';
    if (strpos($string1, $coin) !== FALSE) {
        echo "ETH print";
        $found = true;
    }
    $string2 = 'DOGE';
    if (strpos($string2, $coin) !== FALSE) {
        echo "DOGE print";
        $found = true;
    }
    $string3 = 'WAVES';
    if (strpos($string3, $coin) !== FALSE) {
        echo "WAVES print";
        $found = true;
    }
}
if (!$found) {
    echo "Not found!";
}
如果您在函数中,则可以添加:

return $found;

返回将打破您的循环。试试这个:

$coins = array("BTC", "ETH", "DOGE", "WAVES");

$found = false;
foreach ($coins as $coin) {
    $string = 'BTC';
    if (strpos($string, $coin) !== FALSE) {
        echo "BTC print";
        $found = true;
    }
    $string1 = 'ETH';
    if (strpos($string1, $coin) !== FALSE) {
        echo "ETH print";
        $found = true;
    }
    $string2 = 'DOGE';
    if (strpos($string2, $coin) !== FALSE) {
        echo "DOGE print";
        $found = true;
    }
    $string3 = 'WAVES';
    if (strpos($string3, $coin) !== FALSE) {
        echo "WAVES print";
        $found = true;
    }
}
if (!$found) {
    echo "Not found!";
}
如果您在函数中,则可以添加:

return $found;

如果您的打印线仅在硬币名称上不同,则可以使用简单的循环:

$coins = ['BTC', 'ETH', 'DOGE', 'WAVES'];

foreach ($coins as $coin) {
    echo "$coin print", PHP_EOL;
}

以下是。

如果您的打印线仅在硬币名称上不同,您可以使用一个简单的循环:

$coins = ['BTC', 'ETH', 'DOGE', 'WAVES'];

foreach ($coins as $coin) {
    echo "$coin print", PHP_EOL;
}

这是。

删除
返回
s。它只循环一次,因为您返回了它。请删除
return
s。它只循环一次,因为你把它退回了。