简体   繁体   中英

How to remove starting / ending square brackets from array

In my previous question I was able to generate the array I was looking for. At this moment I am struggling with the starting blockquote. The block quote at the beginning and at the end has to be removed.

My code:

$options['chart']    = array('type' => 'line');
$options['title']    = array('text' => 'Monthly report');
$options['subtitle'] = array('text' => 'Milage');
$options['xAxis']    = array('categories' => "Jan, Feb, 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'");

while($row = $result->fetch(PDO::FETCH_ASSOC)){
    $data1[] = $row['automillage'];
    $data2[] = $row['dealer_status'];
}

$naam1 = array('name' => 'Data series 1');
$naam2 = array('name' => 'Data series 2');

$dataset1['data'] = $data1;
$dataset2['data'] = $data2;

$series1[] = $naam1+$dataset1;
$series2[] = $naam2+$dataset2;

$mergeData1['series'] =  array_merge($series1,$series2);

$join[] = json_decode(json_encode($options), true);
$join[] = json_decode(json_encode($mergeData1), true);

echo json_encode($join,JSON_NUMERIC_CHECK);

My output https://ibb.co/n2vM76

What can I do to delete the first and last blockquote

So first, those aren't blockquotes. They're square brackets, and they indicate an array, which $join is (I assume you know that). You can't get rid of them, because it is an array. If you're looking to merge the two associative array you have into one array, instead of creating an array of two arrays, the best raw method (in my opinion) would be to loop through one object, and assign it's properties by key to the other. Of course, since you're setting all the values anyway, you could just set them all into one array from the start. Bare in mind that as you're creating an associative array, which doesn't exist in JSON, it translates that into an object.

As long as your arrays have different keys, you can also use array_merge or add the two arrays together ( $array1 + $array2 ).

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