简体   繁体   中英

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

I am trying to insert an array into a two-dimensional array. But whatever I try - inserting it as an array, as text, array_push, ... - it won't do what I want.

I have this piece of code which describes the content and look-and-feel of a graph:

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

Now, based on a parameter passed through the JSON call, it could be that the border of the graph shall be displayed (default is "no border").

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

Now, how do I get the $border into the " chart "-part of $data_ ?

I tried something like this (by defining the $border before the $data_ ):

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

and like this:

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

(plus many others). But this always results - obviously and understandably - in $border being inserted as an array in "chart", and not having the params at the same level, that is:

I get this:

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

instead of this:

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

I guess the solution is pretty simple, but unfortunately not known to me. Thanks a lot for any hints!

I'd try with $data_['chart'] = array_merge($data_['chart'], $border);

An example:

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

Displays:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM