【问题标题】:How to add an array to an existing two-dimensional array (for use in Highcharts)如何将数组添加到现有的二维数组(用于 Highcharts)
【发布时间】:2015-02-02 08:06:06
【问题描述】:

我正在尝试将数组插入二维数组。但无论我尝试什么 - 将其作为数组、文本、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), 

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

【问题讨论】:

    标签: php arrays highcharts


    【解决方案1】:

    我会尝试$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)
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2012-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多