如何将数组添加到现有的二维数组(用于高图表)


How to add an array to an existing two-dimensional array (for use in Highcharts)

>我正在尝试将数组插入二维数组。但无论我尝试什么 - 将其作为数组插入,作为文本,array_push,...- 它都不会做我想要的。

我有这段代码,它描述了图表的内容和外观:

$data_ = array( "chart"  => array("renderTo" => "container", "type" => "spline", 
                                     "zoomType" => "xy"), 
                "tooltip"   => array("shared" => true, "crosshairs" => true),
                "series"    => $data);

现在,根据通过 JSON 调用传递的参数,可能会显示图形的边框(默认值为"无边框")。

if ($_GET["border"] == true)
{
    $border = array("borderWidth" => 1, "borderRadius" => 5, "borderColor" => "#666", "shadow" => true);
}

现在,如何将$border放入"图表"中 - $data_的一部分?

我尝试了这样的事情(通过在 $data_ 之前定义$border):

$data_ = array( "chart"  => array("renderTo" => "container", "type" => "spline", 
                                     "zoomType" => "xy", $border), 
                "tooltip"   => array("shared" => true, "crosshairs" => true),
                "series"    => $data);

像这样:

$data_["chart"][] = $border;

(加上许多其他人)。但这总是导致 - 显然且可以理解 - $border作为数组插入"图表",并且没有在同一级别上具有参数,即:

我明白这个:

$data_ = array( "chart"  => array("renderTo" => "container", "type" => "spline", 
                                     "zoomType" => "xy", 
                                     array("borderWidth" => 1, "borderRadius" => 5, "borderColor" => "#666", "shadow" => true)), 

取而代之的是:

$data_ = array( "chart"  => array("renderTo" => "container", "type" => "spline", 
                                     "zoomType" => "xy", 
                                     "borderWidth" => 1, "borderRadius" => 5, "borderColor" => "#666", "shadow" => true), 

我想解决方案非常简单,但不幸的是我不知道。非常感谢任何提示!

我会尝试使用$data_['chart'] = array_merge($data_['chart'], $border);

举个例子:

$a = array("chart" => array(1, 2, 3));
$b = array(4, 5, 6);
$a['chart'] = array_merge($a['chart'], $b);
var_dump($a['chart']);

显示:

array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}