将数组附加到多维数组无法静默PHP?

时间:2014-03-15 09:32:59

标签: php arrays

我正在尝试将数组附加到多维数组中的另一个数组:

这是多维数组:

 $info[] = array(
     'key' => $row['id'],
     'master' => array(
         'name' => $row['master_name'],
         "detail" => array()
     )
);

我有一个主ID,一个主项目,一个带有名称的数组和另一个带有详细信息的数组(第一次是空的)。

但是,当我尝试在$ info [' master'] [' detail']数组中添加另一个带有详细信息的数组时,如下所示:

  $info['master']['detail'][] = array("name" => "A detail name", 
                                      "value" => "A detail value");

什么都没有添加......这怎么可能?

编辑:应该向主人添加详细信息的foreach循环:

foreach ($details as $detail)
{

        $name = $detail['detail_name'];
        $value = $detail['detail_value'];

        if ($info['key'] == $detail['id']) {
              $info['master']['detail'][] = array("name" => $name,
                                                  "value" => $value);

        }
 }

1 个答案:

答案 0 :(得分:0)

我不确定我理解但是当我看到你的例子时,我认为这是一个索引问题:
尝试替换

 $info[] = array(
     'key' => $row['id'],
     'master' => array(
         'name' => $row['master_name'],
         "detail" => array()
     )
);
  $info['master']['detail'][] = array("name" => "A detail name", 
                                      "value" => "A detail value");

通过

$info = array( 'key' => $row['id'],
               'master' => array('name' => $row['master_name'],, 
                                 "detail" => array())
                );

  $info['master']['detail'] = array("name" => "A detail name", 
                                    "value" => "A detail value");

并添加新值:

$info['master']['detail']['foo'] = "A detail foo";