在PHP中,我们知道使用此代码创建关联数组
$variable = array('0001'=>'value1', '0010'=>'value2');
Run Code Online (Sandbox Code Playgroud)
并使用此代码打印所有键和值
foreach($variable as $key1 => $val1)
foreach($val1 as $key2 => $val2)
echo ("$key2 => $val2 <br />")
Run Code Online (Sandbox Code Playgroud)
问题是如何在vb.net中执行此操作?
我知道使用这个在vb.net中创建关联数组:
Dim var As New Collection
var.Add("value1", "0001")
var.Add("value2", "0010")
Run Code Online (Sandbox Code Playgroud)
如何在PHP中打印像foreach这样的vb.net中的值和键?谢谢
Tim*_*ter 12
虽然我不熟悉PHP(不再),但我认为关联数组相当于一个HashTable
或更现代的强类型Dictionary
:
Dim dict = New Dictionary(Of String, String)
dict.Add("value1", "0001")
dict.Add("value2", "0010")
Run Code Online (Sandbox Code Playgroud)
通常你会查找键:
Dim val2 = dict("value2") ' <-- 0010
Run Code Online (Sandbox Code Playgroud)
但是如果你想枚举它(效率较低):
For Each kv As KeyValuePair(Of String, String) In dict
Console.WriteLine("Key:{0} Value:{1}",kv.Key, kv.Value)
Next
Run Code Online (Sandbox Code Playgroud)