dongzine3782 2019-03-12 19:15 采纳率: 0%
浏览 226
已采纳

将PHP数组转换为C#

I'm needing help converting the php array in red below. I've been able to convert some of the arrays to dictionaries but not sure what the best solution is since this is an array inside of an array .

$reg_rows=array();
$rc_count=0;
$reg_counter=0;
$hold_achtrndt="1970-01-01";
$hold_regpay_sort="0";
while ( ($row_rc=sqlsrv_fetch_assoc($result_rc)) ) 
    {
    if ( strtoupper(substr($row_rc['tb_value1'],4,1))=="Y" )
          $reg_counter=count($reg_rows);  //If the paymethod is set to require it's own deposit, increment
    elseif ( $row_rc['regpay_sort']!=$hold_regpay_sort ) 
    {
        $reg_counter=count($reg_rows);  //If the paymethod changes, increment so each different type is separated
        $hold_regpay_sort=$row_rc['regpay_sort'];
    }
    **$reg_rows[$reg_counter][$row_rc['rc_code']]=$row_rc;** //THIS IS THE ARRAY I NEED HELP WITH
    $rc_count++;
}

Maybe it would be easier if I showed you what I'm trying to get.Example below. where 0 and 1 would be the reg_counter and 5514,5115,5116 would be the $row_rc['rc_code'] and the array[78] are the fields returned from SQL table.

$reg_rows[0][5515]{array[78]}
$reg_rows[1][5514]{array[78]}
$reg_rows[1][5516]{array[78]}
  • 写回答

1条回答 默认 最新

  • douraoyw194498 2019-03-12 20:15
    关注

    Hi trev52 and welcome to Stackoverflow!

    As you said, you have an associative array¹ inside the values of an array.

    In C#, you would have a dictionary placed inside the values of an array, or inside the values of an outer dictionary. More precisely, if we knew the size of the outer array in advance, we could write

    var size = 42; // let's assume we know this somehow
    var outer = new Dictionary<InnerKey, InnerValue>[size];
    for (int i = 0; i < size; i++) {
      for (key in some_source) {
        var value = some_function(key);
        if (outer[i] == null) {
          outer[i] = new Dictionary<InnerKey, InnerValue>();
        }
        outer[i].Add(somekey, somevalue);
      }
    }
    

    However, in this case we don't know the size of the array in advance, and resizing the array on the fly in C# is going to be a pain, so we'll use a dictionary whose values are dictionaries themselves:

    var outer = new Dictionary<Int, Dictionary<InnerKey, InnerValue>>;
    for (int i = 0; i < size; i++) {
      for (key in some_source) {
        var value = some_function(key);
        if (!outer.ContainsKey(i)) {
          outer.Add(i, new Dictionary<InnerKey, InnerValue>());
        }
        outer[i].Add(somekey, somevalue);
      }
    }
    

    Note that you can also create elements in dictionaries with d[k] = v. If the key already exists d[k] = v will overwrite its value, but d.Add(k, v) will raise an exception, therefore it is safer to use Add if you know the keys are supposed to be unique.

    For clearer code, at the cost of some slightly more complex features, you could get rid of the manual instantiation of the inner dictionaries using a DefaultDictionary. You could also quite likely get rid of the loops by using LINQ. It looks a lot like SQL with some C# syntax, and is able to manipulate C# lists, dictionaries, arrays and so on. The SQL binding that you are using might even have a LINQ interface!

    ¹ An arrays which keys are not integers is usually called an associative array (it associates a value to a key).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?