dongzine3782 2019-03-12 19:15
浏览 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).

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!