我的 PHP foreach 命令仅作用于数组的最后一项!

发布于 2024-11-01 20:43:43 字数 627 浏览 5 评论 0原文

我从文本字段获取输入,将每一行转换为数组中的一个值,然后使用 foreach 循环对每个值运行搜索和替换。代码如下:

$countryinp = trim($_POST['country']);
$countryinp = explode("\n", $countryinp);
$countryinp = array_filter($countryinp, 'trim');
foreach($countryinp as $code)
{
$country = '';
if( $code == 'Afghanistan' ) $country = 'AF';
if( $code == 'Aland Islands' ) $country = 'AX';
if( $code == 'Albania' ) $country = 'AL';
if( $code == 'Algeria' ) $country = 'DZ';
...
if( $country == '') $country = $code;
echo "$country";
echo "<br />";
unset($code);
}

该代码将循环并输出所有输入的列表,但它仅将输入的最后一行的 $country 设置为两个字母的代码!我快疯了,有人见过这个吗?

I'm taking the input from a text field, turning each line into a value in an array, and then using a foreach loop to run a search and replace on each value. Code is as follows:

$countryinp = trim($_POST['country']);
$countryinp = explode("\n", $countryinp);
$countryinp = array_filter($countryinp, 'trim');
foreach($countryinp as $code)
{
$country = '';
if( $code == 'Afghanistan' ) $country = 'AF';
if( $code == 'Aland Islands' ) $country = 'AX';
if( $code == 'Albania' ) $country = 'AL';
if( $code == 'Algeria' ) $country = 'DZ';
...
if( $country == '') $country = $code;
echo "$country";
echo "<br />";
unset($code);
}

The code will loop and output a list of all the inputs, but it only sets $country to a two letter code for the last line of the input! I'm going crazy, anyone seen this before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城°AllureLove 2024-11-08 20:43:43

尝试这样的事情......

foreach($countryinp as $code)
{
$code = trim($code);
    switch($code){
        case "Afganistan": $country = "AF"; break;
        case "Alanad Islands": $country = "AX"; break;
        default: $country = $code;
    }

    echo $country."<br />";
}

并确保你的数据存在......

Try something like this...

foreach($countryinp as $code)
{
$code = trim($code);
    switch($code){
        case "Afganistan": $country = "AF"; break;
        case "Alanad Islands": $country = "AX"; break;
        default: $country = $code;
    }

    echo $country."<br />";
}

And make sure your data exists...

临走之时 2024-11-08 20:43:43

您到底遇到了什么问题?你说它只将最后一行的 $country 设置为两个字母的代码,但它是否打印出了之前的所有代码?

这应该是显而易见的,但如果您只是在循环结束后遇到 $country 的值问题,那么您应该意识到 $country 变量被覆盖循环的每次迭代,因此仅在循环结束时保留上次迭代的值。

如果问题是您的循环没有打印每个代码,那么这听起来像是一个数据问题,您需要向我们显示数组中的值才能确定问题。

您可能遇到的一个可能的数据问题是这一行:

$countryinp = array_filter($countryinp, 'trim');

我认为您使用这一行的目的是将 trim 函数应用于数组中的每个元素,但这不是 array_filter做。 array_filter的作用是将数组中的每个元素传递给回调函数。该函数检查元素的内容,如果应保留内容则返回 true ,如果应从数组中过滤该元素则返回 false 。

当您将 trim 传递给 array_filter 时,唯一发生的事情是任何空数组元素或仅包含空格的数组元素都将被删除。这是因为这些元素被修剪后,它们是空的。 PHP 将空字符串解释为 false 并将其从数组中删除。

这可能会导致您的 if 语句出现问题,因为如果 $code 包含“Afghanistan”(注意尾随空格),那么它就不会被if 语句,因为字符串不匹配。

您最好在 foreach 循环的开头运行 $code = trim($code); 行。

另外,你的代码对我来说有点难看。如果您只是要一遍又一遍地检查同一个变量(即重复的 if ($code == 'Value') 语句),那么您确实应该使用 switch 语句改为:

switch ($code) {
    case "Afghanistan":
        $country = 'AF';
        break;
    case "Aland Islands":
        $country ='AX';
        break;
    // repeat for other cases
    default:
        $country = $code;
}

What problem are you having exactly? You say that it only sets $country to a two-letter code for the last line, but is it printing out all of the codes before that?

This should be obvious, but if you're only having problems with the value of $country after the loop finishes, then you should realize that the $country variable is overwritten on each iteration of the loop, and would therefore only hold the value from the last iteration when the loop finishes.

If the problem is that your loop isn't printing each code, then that sounds like it's a data problem, and you would need to show us the values in your array in order to determine the problem.

One possible data problem you may have is this line:

$countryinp = array_filter($countryinp, 'trim');

I think your intent with this line is to apply the trim function to each element in the array, but that's not what array_filter does. The function of array_filter is to pass each element in an array to a callback function. That function examines the contents of the element, then returns true if the contents should be kept, or false if that element should be filtered out of the array.

When you pass trim to array_filter, the only thing that happens is that any array element which is empty, or only contains whitespace, will be removed. This is because after such elements are trimmed, they are empty. PHP interprets an empty string as false and removes it from the array.

This could cause issues with your if statements, because if $code contains, for example, "Afghanistan " (note the trailing space), then it wouldn't get caught by the if statement as the strings wouldn't match.

You would be much better off simply running the line $code = trim($code); at the beginning of your foreach loop.

Also, your code is a little bit ugly to me. If you're just going to check the same variable over and over (i.e. repeated if ($code == 'Value') statements), then you should really be using a switch statement instead:

switch ($code) {
    case "Afghanistan":
        $country = 'AF';
        break;
    case "Aland Islands":
        $country ='AX';
        break;
    // repeat for other cases
    default:
        $country = $code;
}
阪姬 2024-11-08 20:43:43

这段代码可能有太多逻辑问题

foreach($countryinp as $code) // #1
{
$country = ''; // #2
if( $code == 'Afghanistan' ) $country = 'AF'; // #3
if( $code == 'Aland Islands' ) $country = 'AX';
if( $code == 'Albania' ) $country = 'AL';
if( $code == 'Algeria' ) $country = 'DZ';
...
if( $country == '') $country = $code; #4
echo "$country";
echo "<br />";
unset($code); // #5
}
  1. $code 在 foreach 中是可见的变量
    范围,赋值为
    next($countryinp) (指向下一个的指针
    数组中的元素)

  2. $country 重置为 ''(空
    值)在每次迭代(你是
    重置 $country 值)

  3. $code == 'xxxx' - 在这里您可以使用
    switch() 语句,有两个好处:1)
    表达式仅计算一次
    然后与“case”进行匹配,2)
    可维护性(如果你必须
    更改代码中的某些内容...
    更好的解决方案是简单地
    有以国家/地区名称作为键的数组
    和国家代码作为值,那么你
    可以直接获取国家代码
    像这样
    $countryCodes[$countryName];

  4. 您可以在此处指定当前国家/地区
    $code 至 $country 仅当 $country
    是空的,但正如 #2 中提到的
    正在重置每个国家的 $country 值
    iteraction

  5. 这里取消设置 $code 的值
    这不是必需的,$code 是
    仅在 foreach() 内可见并且
    当您的代码自动取消设置
    存在 foreach 循环。

希望有帮助。 :)

You can too many logical issues with this piece of code

foreach($countryinp as $code) // #1
{
$country = ''; // #2
if( $code == 'Afghanistan' ) $country = 'AF'; // #3
if( $code == 'Aland Islands' ) $country = 'AX';
if( $code == 'Albania' ) $country = 'AL';
if( $code == 'Algeria' ) $country = 'DZ';
...
if( $country == '') $country = $code; #4
echo "$country";
echo "<br />";
unset($code); // #5
}
  1. $code is variable visible in foreach
    scope, assigned with value of
    next($countryinp) (pointer to next
    element in array)

  2. $country is reset to '' (empty
    value) on each iteration (you are
    resetting $country value)

  3. $code == 'xxxx' - here you can use
    switch() statement, two benefits: 1)
    expression is evaluated only once
    then matched against 'case' and 2)
    maintainability (if you have to
    change something in your code...
    Better solution would be to simply
    have array with country name as key
    and country code as value, then you
    can directly access the country code
    like this
    $countryCodes[$countryName];

  4. Here you assign current country
    $code to $country only if $country
    is empty but as mantioned in #2 you
    are resetting $country value on each
    iteraction

  5. Here you unset the value of $code
    and this is not necessary, $code is
    only visible inside foreach() and
    automatically unset when your code
    exists the foreach loop.

Hope that helps. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文