Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Foreach加整数_C#_Php - Fatal编程技术网

C# Foreach加整数

C# Foreach加整数,c#,php,C#,Php,如果问题已经得到回答,我表示歉意,但我没有找到任何答案 在C#中,我在字典上有一个基本的“foreach循环” foreach(var item in _dict) { if(item.Key == "test") { item.Value++; // This line will add one more number to the integer 1+1 } } PHP中的等效代码是什么 PHP 然而,在C#中,追加在字典中是成功的,但在PHP

如果问题已经得到回答,我表示歉意,但我没有找到任何答案

在C#中,我在字典上有一个基本的“foreach循环”

foreach(var item in _dict)
{        
   if(item.Key == "test")
   {
      item.Value++; // This line will add one more number to the integer 1+1
   }
}
PHP中的等效代码是什么

PHP

然而,在C#中,追加在字典中是成功的,但在PHP数组中不是,即使代码成功运行,初始值也保持不变


很抱歉,我的问题很棘手,但我不太使用PHP,只有asp.net和C#

您必须告诉PHP使用$cl作为引用变量,使用&before

foreach($Clients as &$cl)
{
   if($cl["ChannelID"] == $chanID)
   {
     $cl['Clients']++;
   }
}

您必须告诉PHP使用$cl作为引用变量,在它之前使用&before

foreach($Clients as &$cl)
{
   if($cl["ChannelID"] == $chanID)
   {
     $cl['Clients']++;
   }
}
这个怎么样

foreach($Clients as $cl)
    {
       if($cl["ChannelID"] == $chanID)
       {
         $id = (int)$cl['Clients'];
         $id =  $id++;
         echo $id; // added just for test 
       }
    }
这个怎么样

foreach($Clients as $cl)
    {
       if($cl["ChannelID"] == $chanID)
       {
         $id = (int)$cl['Clients'];
         $id =  $id++;
         echo $id; // added just for test 
       }
    }

请看这个问题,答案是:“Foreach在循环之前复制数组的结构,所以您不能更改数组的结构并在循环中等待新元素。您可以使用
while
而不是
Foreach
”。我也不做PHP,但我认为您需要
$Clients++
,因为这是您在
foreach
语句中定义的字段。您的C#code可以是
if(_dict.ContainsKey(“test”))_dict[“test”}++
,因为字典中最多只能有一个带有该键的条目。请参阅此问题的答案说明:Foreach在循环之前复制数组的结构,因此您不能更改数组的结构并在循环中等待新元素。您可以使用
while
而不是
Foreach
。“我也不做PHP,但我认为您需要
$Clients++
,因为这是您在
foreach
语句中定义的字段。您的C代码可以是
if(_dict.ContainsKey(“test”)_dict[“test”}++
由于字典中最多只能有一个带有该键的条目。有效!非常感谢,先生。我不知道“&”符号。谢谢!@user3280998如果您想了解有关
&
符号的更多信息,请阅读并引用以下重要内容:“为了能够直接修改循环中的数组元素,在$value之前加上&。在这种情况下,值将通过引用分配。”行!非常感谢先生。不知道“&”符号。谢谢!@user3280998如果您想了解有关
&
符号的更多信息,请阅读和重要引用:”为了能够直接修改循环中的数组元素,在$value之前加上&。在这种情况下,值将通过引用分配。“这并不是他所需要的,它实际上与他的代码一样。这并不是他所需要的,它实际上与他的代码一样。