Excel VBA中的多维关联数组

我正在尝试在Excel VBA中创建包含'php'中的数据的变量</ p> \ n

  $ classroom ['group1'] ['sub_group1'] ['John'] ['math'] ['pretest'] = 20; 
$ classroom ['group1'] [' sub_group1'] ['John'] ['math'] ['posttest'] = 50;
$ classroom ['group1'] ['sub_group1'] ['John'] ['science'] ['pretest'] = 26;
$ classroom ['group1'] ['sub_group1'] ['John'] ['science'] ['posttest'] = 45;

$ classroom ['group1'] ['sub_group1' ] ['David'] ['math'] ['pretest'] = 40;
$ classroom ['group1'] ['sub_group1'] ['David'] ['math'] ['posttest'] = 44 ;
$ classroom ['group1'] ['sub_group1'] ['David'] ['science'] ['pretest'] = 15;
$ classroom ['group1'] ['sub_group1'] ['David ''['science'] ['posttest'] = 17;

$ classroom ['group1'] ['sub_group2'] ['Kate'] ['math'] ['pretest'] = 15; \ n $ classroom ['group1'] ['sub_group2'] ['Kate'] ['math'] ['posttest'] = 30;
$ classroom ['group1'] ['sub_group2'] ['Kate'] ['science'] ['pretest'] = 26;
$ classroom ['group1'] ['sub_group2'] ['Kate'] ['science'] ['posttest'] = 40;

$ classroom ['group1'] ['sub_group2'] ['Alice'] ['math'] ['pretest'] = 14;
$ classroom ['group1'] ['sub_group2' ] ['Alice'] ['math'] ['posttest'] = 35;
$ classroom ['group1'] ['sub_group2'] ['Alice'] ['science'] ['pretest'] = 28 ;
$ classroom ['group1'] ['sub_group2'] ['Alice'] ['science'] ['posttest'] = 42;

echo json_encode($ classroom);
</ code> < / pre>

我希望它的结果应该是层状结构,如下面的图像</ p>

在此处输入图像说明 </ p>

我尝试使用字典,但我的数据有重复密钥。</ p> \ n

我应该怎么做才能得到我想要的结果以及我应该使用什么样的变量类型?</ p>

谢谢。</ p> \ n </ div>

展开原文

原文

I'm trying to create variable in Excel VBA that contain data like this in 'php'

$classroom['group1']['sub_group1']['John']['math']['pretest'] = 20;
$classroom['group1']['sub_group1']['John']['math']['posttest'] = 50;
$classroom['group1']['sub_group1']['John']['science']['pretest'] = 26;
$classroom['group1']['sub_group1']['John']['science']['posttest'] = 45;

$classroom['group1']['sub_group1']['David']['math']['pretest'] = 40;
$classroom['group1']['sub_group1']['David']['math']['posttest'] = 44;
$classroom['group1']['sub_group1']['David']['science']['pretest'] = 15;
$classroom['group1']['sub_group1']['David']['science']['posttest'] = 17;

$classroom['group1']['sub_group2']['Kate']['math']['pretest'] = 15;
$classroom['group1']['sub_group2']['Kate']['math']['posttest'] = 30;
$classroom['group1']['sub_group2']['Kate']['science']['pretest'] = 26;
$classroom['group1']['sub_group2']['Kate']['science']['posttest'] = 40;

$classroom['group1']['sub_group2']['Alice']['math']['pretest'] = 14;
$classroom['group1']['sub_group2']['Alice']['math']['posttest'] = 35;
$classroom['group1']['sub_group2']['Alice']['science']['pretest'] = 28;
$classroom['group1']['sub_group2']['Alice']['science']['posttest'] = 42;

echo json_encode($classroom);

the result I want it to be should be heirarchy structure as the image below

enter image description here

I tried to use dictionary but my data have duplicated key.

What should I do in order to get the result I want it to be and what kind of variable type should I use?

Thank you.

duangangpin078794
duangangpin078794 如果你有json数据并且需要在VBA中解析它们,那么已经存在一些用于VBA的json解析器,例如这个。
大约 3 年之前 回复
dsfsdfsd34324
dsfsdfsd34324 您可以使用简单的VBA.Collection类型。为组创建自己的类,该组将包含子组的集合,这些子组将包含收集主题集合等的人员集合:)。字典也是可能的,但我猜这里收集就足够了。为了能够将项目添加到字典中,尽管每个项目都必须具有唯一ID。如果是VBA.Collection,您也可以使用密钥,但是您可以像使用包一样使用Collection来处理某些项目。但在这种情况下,您无法通过密钥访问它们,但您必须一个接一个地枚举它们。
大约 3 年之前 回复
dro80463
dro80463 如果您希望在Excel中使用这些数据,建议您牢记Excel基本上是三维的,工作表,列和行。通过使用不同的工作簿可以实现第四个维度。但是,您描述的任务可以在单个工作表上轻松解决,这意味着两个维度,即二维数组。
大约 3 年之前 回复
drasebt1835
drasebt1835 将代码包含在问题中可能是最好的。这样我们就可以帮助您修复它以使其正常工作。
大约 3 年之前 回复
dougu4448
dougu4448 问题是......为什么你的词典有重复的密钥?(它在哪个级别有它?)第一个词典应该有一个键(“group1”)。作为“group1”值的Dictionary应该有两个键(“sub_group1”和“sub_group2”)。作为“sub_group1”值的字典应该有两个键(“John”和“David”)。“John”字典和“David”字典都应该有两个键(“math”和“science”)等。这些Dictionary对象都不会有重复键。
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问