dongzine3782 2019-03-12 11: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 12: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).

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部