PHP - 通过调用 returns 键和值的方法插入数组

PHP - Inserting to an array by calling a method that returns both key and value

我试图在构建数组时调用方法。我正在构建一个相当大的配置数组,其中包含许多可重复使用的块。

这是我想要得到的数组:

array(
   "masterKey" => array(
      "myKey" => array(
         "valueField" => "hi"
      ),
      "anotherKey" => array(
         "valueField" => "hi again"
      )
      ....
   )
);

这就是我想要的生成方式:

array(
   "masterKey" => array(
      self::getValueField("myKey", "hi"),
      self::getValueField("anotherKey", "hi again"),
      ...
   )
);
private static function getValueField($key, $value)
{
   return array($key => 
      "valueField" => $value
   );
}

但这给了我

array(
   "masterKey" => array(
      [0] => array(
         "myKey" => array(
            "valueField" => "hi"
         )
      ),
      [1] => array(
         "anotherKey" => array(
           "valueField => "hi again"
         )
      )
   )
);

不是将 "masterKey" 字段构造为文字,而是合并 self::getValueField 返回的数组:

array(
   "masterKey" => array_merge(
       self::getValueField("myKey", "hi"),
       self::getValueField("anotherKey", "hi again"),
       ...
    )
);

只是想补充一点,为了让@giaour 的回答起作用,getValueField 函数的代码应该是:

<?php
private static function getValueField($key, $value)
{
    return array(
        $key => array(
            "valueField" => $value
        )
    );
}