简体   繁体   English

Foreach在php中不起作用

[英]Foreach doesn't work in php

I have a code like this: 我有这样的代码:

<?php

$kode ["J"]= array (20, C, D, F);
$kode ["K"]= array (50, B, G, U);
$kode ["T"]= array (70, V, W);

function kota ($start, $end){
    if (is_array($kode)) {
        foreach ($kode as $kota => $path){
            if ($kota=$end) {
                for ($i=1; $i < count ($kota); $i++){
                    $jalur=$start.$path[$i];
                }
            }
        }
        return $jalur;
    }
}
$start = "J";
$end = "T";
$hasil=kota ($start, $end);
echo "".$hasil;
?>

I want the output to be JVW 我希望输出为JVW
I don't know what is wrong, can anyone help me? 我不知道怎么了,有人可以帮我吗? please... 请...

Syntatical errors 语法错误

Looks like you forgot to use the equality operator == 看起来您忘记了使用等号运算符==

if ($kota = $end){ ... }

Should be - 应该 -

if ($kota == $end){ ... }

By using only one equals sign you are actually assigning a value to $kota , not comparing the value to $end as should be done in conditional expressions. 通过仅使用一个等号,您实际上是在为$kota 分配一个值,而不是像在条件表达式中那样其与$end 进行比较

I don't think this is the only thing that is causing trouble here.. but it definitely should be sorted out :) 我不认为这是唯一在这里引起麻烦的事情。但是绝对应该对它进行梳理:)


Variable scope 可变范围

Another thing I noticed in your code is that you are referencing variables within the kota function that were not defined in it's scope. 我在代码中注意到的另一件事是,您引用的是kota函数中未在其作用域中定义的变量。 This means that the $kota array is not accessible within the kota function. 这意味着在kota函数中无法访问$kota数组。 You should pass the $kota array to the function so that you can use it within scope of the function. 您应该将$kota数组传递给函数,以便可以在函数范围内使用它。 Here is some more info on variable scopes in PHP . 这是有关PHP中变量作用域的更多信息


Naming conventions 命名约定

One final note on your variable name choice... You should possibly think of changing the variable $kota or function kota so that their names are not identical. 关于变量名选择的最后一点...您可能应该考虑更改变量$kota或函数kota以使它们的名称不同。 This will help improve readability and perhaps prevent some mistakes at 4am when you've been debugging the whole night ;) 这将有助于提高可读性,并可能在整夜调试时防止凌晨4点出现一些错误;)

On the line 在线上

if ($kota=$end){

you are not comparing, but overwriting the value in $kota, and that is always true. 您不是在进行比较,而是覆盖$ kota中的值,这始终是正确的。

Also the $kode is not available in the function scope, try adding it to the parameter list, or using global (not advised). 同样, $kode在功能范围中不可用,请尝试将其添加到参数列表中,或使用global (不建议使用)。

You need to either pass $kode into your function as an argument or call global $kode; 您需要将$kode作为参数传递给函数,或者调用global $kode; inside your function. 内部功能。 I'd recommend the former. 我建议前者。

Additionally, if ($kota=$end) needs to be if ($kota==$end) as others have mentioned. 另外, if ($kota=$end)需要成为if ($kota==$end)就像其他人提到的那样。

Not sure, that $jalur=$start; 不确定$jalur=$start; is on the correct place, but this script gives what you want: 在正确的位置,但是此脚本提供了您想要的:

<?php

$kode ["J"]= array (20, 'C', 'D', 'F');
$kode ["K"]= array (50, 'B', 'G', 'U');
$kode ["T"]= array (70, 'V', 'W');

function kota ($start, $end){
      global $kode;

      if(is_array($kode)){

            foreach ($kode as $kota => $path){

                  if ($kota == $end){
                        $jalur=$start;
                        for ($i=1; $i < count ($path); $i++){
                              $jalur .= "-" . $path[$i];
                        }
                  }
            }
            return $jalur;
      }
}
$start  = "J";
$end    = "T";
$hasil=kota ($start, $end);
echo $hasil;
?>

Your code has lots of issues 您的代码有很多问题

  1. As others pointed out, in if ($kota=$end) { , you are assigning $end to $kota and always return true ie the code in the IF clause always execute 正如其他人指出的那样,在if ($kota=$end) { ,您将$end分配给$kota并始终返回true即IF子句中的代码始终执行

  2. PHP has function scope. PHP具有功能范围。 Simply put, variables declared inside a function cannot be used outside, and vice versa. 简而言之,在函数内部声明的变量不能在外部使用,反之亦然。 Use parameters to pass your $kode into the function. 使用参数将$kode传递给函数。

  3. Use of bare strings ie V and W in $kode ["T"]= array (70, V, W); 使用裸字符串,即$kode ["T"]= array (70, V, W); VW $kode ["T"]= array (70, V, W); and other places. 和其他地方。 This is highly recommended against, and PHP does warn you about this. 强烈建议您这样做,PHP会对此发出警告。

  4. As other pointed out, $jalur=$start.$path[$i]; 正如其他指出的那样, $jalur=$start.$path[$i]; would overwrite $jalur every time. 每次都会覆盖$jalur The for-loop outside is meaningless. 外部的for循环是没有意义的。 You would use .= the append-to operator. 您将使用.=附加操作符。 Note that you also need to initialize your variable before using this operator. 请注意,在使用此运算符之前,您还需要初始化变量。

  5. $kota is always a string in your code, because in a foreach loop, the variable before => symbol means get the key of the array, and array keys can only be either String or integer. $kota始终是代码中的字符串,因为在foreach循环中, =>符号前的变量表示获取数组的键,并且数组键只能是String或整数。 That said, for ($i=1; $i < count ($kota); $i++){ is meaningless because count($kota) cannot be greater than 1 - your for loop actually never runs. 就是说, for ($i=1; $i < count ($kota); $i++){是没有意义的,因为count($kota)不能大于1-for循环实际上永远不会运行。

  6. This is blatantly meaningless to append a variable with an empty string as in echo "".$hasil; 像在echo "".$hasil;那样,在变量后附加一个空字符串是毫无意义的echo "".$hasil;

I guess this is what you want. 我想这就是你想要的。

<?php

$kode ["J"] = array (20, 'C', 'D', 'F');
$kode ["K"] = array (50, 'B', 'G', 'U');
$kode ["T"] = array (70, 'V', 'W');

function kota ($kode, $start, $end){
    $jalur = $start;
    if (is_array($kode)) {
        foreach ($kode as $kota => $path){
            if ($kota == $end) {
                for ($i = 1; $i < count($path); ++$i) {
                    $jalur .= '-' . $path[$i];
                }
            }
        }
        return $jalur;
    }
}
$start = "J";
$end = "T";
$hasil = kota($kode, $start, $end);
echo $hasil;
?>

This code give you the string which starts with $start and all other elements except the first element in $kode[$end] 此代码为您提供以$start开头的字符串以及$kode[$end]第一个元素以外的所有其他元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM