簡體   English   中英

將二維數組 php 轉換為 c# 數組

[英]Converting 2D array php to c# array

I want to convert a php function to c#, but im struggling with arrays in c#, why they're so difficult:/

我的 php function 工作正常

foreach ($request->ips as $ip){
      $i = explode(',', $ip);
      if(count($i) == 5) {
         $group_ips[$i[4]][] = ['ip' => $i[0], 'domain' => $i[1], 'idip' => $i[2], 'idddomain' => $i[3]];
      }
 }

我轉換為 c#

string[][] data = ips.Trim().Split('\n').Select(t => t.Split(',')).ToArray();

var dict = new Dictionary<string, Dictionary<string, object>>();

foreach (string[] item in data)
{
    if (item.Length == 5)
    {
       dict.Add(item[4].ToString(), new Dictionary<string, object>
         {
            {"ip", item[0].ToString() },
            {"domain", item[1].ToString() },
            {"idi", item[2].ToString() },
            {"idd", item[0].ToString() },

         });
    }
}
expect output 
array (size=2)
  2 => 
    array (size=2)
      0 => 
        array (size=4)
          'ip' => string 'xxxx' (length=12)
          'domain' => string 'test.com' (length=8)
          'idip' => string '1' (length=1)
          'idddomain' => string '1' (length=1)
      1 => 
        array (size=4)
          'ip' => string 'xxxxx' (length=24)
          'domain' => string 'change.test.com' (length=15)
          'idip' => string '2' (length=1)
          'idddomain' => string '1' (length=1)
  1 => 
    array (size=2)
      0 => 
        array (size=4)
          'ip' => string 'xxxx' (length=13)
          'domain' => string 'hello.com' (length=9)
          'idip' => string '3' (length=1)
          'idddomain' => string '2' (length=1)
      1 => 
        array (size=4)
          'ip' => string 'xxxxx' (length=24)
          'domain' => string 'pear.hello.com' (length=14)
          'idip' => string '4' (length=1)
          'idddomain' => string '2' (length=1)

但是在嘗試使用 c# 時,我收到此錯誤

System.ArgumentException: '已添加具有相同鍵的項目。'

我的想法用完了有什么幫助嗎?

您錯過了數據結構的一個深度。 這就是你得到ArgumentException的原因。 您可以通過使用數組索引器dict[key] = value來避免異常。 但它會覆蓋以前的值。 相反,在 2 個字典之間添加一個List ,以便每個鍵能夠存儲多個項目。

它們也是{"idd", item[0].ToString() },的錯字。 應該是索引 3

string ips = "1.1.1.1,test.com,1,1,2\n 1.1.1.1,change.test.com,2,1,2\n 1.1.1.1,hello.com,3,2,1\n 1.1.1.1:8001,pear.hello.com,4,2,1\n";
var data = ips.Trim().Split('\n').Select(t => t.Split(',')).ToList();

var dict = new Dictionary<string, List<Dictionary<string, object>>>();

foreach (var item in data)
{
    if (item.Length == 5)
    {
        var key = item[4].ToString();

        if (!dict.ContainsKey(key))
        {
            dict[key] = new List<Dictionary<string, object>>();
        }

        dict[key].Add( new Dictionary<string, object>
        {
            {"ip", item[0].ToString() },
            {"domain", item[1].ToString() },
            {"idip", item[2].ToString() },
            {"idddomain", item[3].ToString() },
        });
    }
}

Linq版本:

string ips = "1.1.1.1,test.com,1,1,2\n 1.1.1.1,change.test.com,2,1,2\n 1.1.1.1,hello.com,3,2,1\n 1.1.1.1:8001,pear.hello.com,4,2,1\n";

var dict= ips.Trim().Split('\n').Select(t => t.Split(','))
    .Where(item => item.Length == 5)
    .GroupBy(item => item[4])
    .ToDictionary(i => i.Key, i => i.Select(item => new Dictionary<string, string>
        {
            {"ip", item[0] },
            {"domain", item[1] },
            {"idip", item[2] },
            {"idddomain", item[3] },
        }).ToList());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM