Visual Basic中的array_fill_keys

时间:2015-08-26 06:30:37

标签: php arrays vbscript vb6

在visualbasic中是否有与php array_fill_keys相同的函数?

在php中我做:

<?php 
$array1 = array( 
    "a" => "first", 
    "b" => "second", 
    "c" => "something", 
    "red" 
); 

$array2 = array( 
    "a" => "first", 
    "b" => "something", 
    "letsc" 
); 

print_r(array_fill_keys($array1, $array2)); 
?> 

如何在VB中执行此操作?

1 个答案:

答案 0 :(得分:1)

万一你真的需要一个array_fill_keys - 相似的功能,并且可以使用字典:

Option Explicit

' stolen from http://php.net/manual/en/function.array-fill-keys.php:
'  Fill a dictionary with values (xValue), specifying keys (aKeys)
'  Fills a dictionary with the value of the xValue parameter, using the values of aKeys as keys.
Function array_fill_keys(aKeys, xValue)
  Dim t : Set t = CreateObject("Scripting.Dictionary")
  Dim k
  For Each k In aKeys
      t(k) = xValue
  Next
  Set array_fill_keys = t
End Function

Sub FEDicO(d, o)
  Dim k
  For Each k In d.Keys
      o.apply d, k
  Next
End Sub

Class cPPrintLn
  Sub apply(d, k)
    WScript.Echo "  ", k, "=>", d(k)
  End Sub
End Class

' $keys = array('foo', 5, 10, 'bar');
' $a = array_fill_keys($keys, 'banana');
' print_r($a);
' The above example will output:
' Array(
'   [foo] => banana
'   [5] => banana
'   [10] => banana
'   [bar] => banana
' )

Dim aKeys : aKeys    = Array("foo", 5, 10, "bar")
Dim dicX  : Set dicX = array_fill_keys(aKeys, "Banana")
WScript.Echo "dicX:"
FEDicO dicX, New cPPrintLn

输出:

cscript 32219456.vbs
dicX:
   foo => Banana
   5 => Banana
   10 => Banana
   bar => Banana