c# - 将二维数组php转换为c#数组
问题描述
我想将一个 php 函数转换为 c#,但我在 c# 中苦苦挣扎,为什么它们如此困难:/
我的 php 函数工作正常
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
. 您可以通过使用数组 indexer 来避免异常dict[key] = value
。但它会覆盖以前的值。相反,List
在 2 个字典之间添加一个,以便每个键能够存储多个项目。
他们也是一个错字{"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() },
});
}
}
林克版本:
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());
推荐阅读
- ios - 为什么我的控制台 po 没有给出错误但相同的代码行使应用程序崩溃
- java - 使用 java 映射一些名称和值
- node.js - 如何将 gulp watch 脚本转换为 gulp 4
- apache-spark - 如何将 Spark 驱动程序日志发送到历史服务器?(独立模式)
- android - Kotlin:导入库时出现 Azure AD B2C 错误
- android - 如何在 Android Espresso 中将页面滚动到最底部?
- mysql - MySQL 拒绝使用正确的凭据进行访问 (Zabbix 4.0)
- c# - 如何在 bot 4 单元测试中模拟用户状态?
- arrays - Jmeter工具创建请求..无法处理数组参数和动态创建的参数
- laravel - 如何将 Gitpod 的 Root url 转换为 https?