关联数组,如在vb.net中的php中

时间:2012-12-05 09:46:08

标签: vb.net

在PHP中,我们知道使用此代码生成关联数组

$variable = array('0001'=>'value1', '0010'=>'value2');

并使用此代码打印所有键和值

foreach($variable as $key1 => $val1)
  foreach($val1 as $key2 => $val2)
    echo ("$key2 => $val2 <br />")

问题是如何在vb.net中执行此操作?

我知道在vb.net中使用这个来创建关联数组:

Dim var As New Collection
var.Add("value1", "0001")
var.Add("value2", "0010")

如何用PHP中的foreach打印vb.net中的值和键?感谢

2 个答案:

答案 0 :(得分:12)

虽然我不熟悉PHP(不再),但我认为关联数组相当于HashTable或更现代的强类型Dictionary

Dim dict = New Dictionary(Of String, String)
dict.Add("value1", "0001")
dict.Add("value2", "0010")

通常你会查找键:

Dim val2 = dict("value2") ' <-- 0010

但是如果你想枚举它(效率较低):

For Each kv As KeyValuePair(Of String, String) In dict
    Console.WriteLine("Key:{0} Value:{1}",kv.Key, kv.Value)
Next

答案 1 :(得分:0)

    Dim row As Dictionary(Of String, Object)
    Dim rows As Dictionary(Of String, Object)
    row = New Dictionary(Of String, Object)
    rows = New Dictionary(Of String, Object)

    row.Add("a", 11)
    row.Add("b", 22)
    rows.Add("ab", row)