简体   繁体   English

如何从数组中删除开始/结束方括号

[英]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. 此时此刻,我正在努力与起步的quotequote。 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 我的输出https://ibb.co/n2vM76

What can I do to delete the first and last blockquote 我该怎么做才能删除第一个和最后一个块引用

So first, those aren't blockquotes. 所以首先,这些不是blockquotes。 They're square brackets, and they indicate an array, which $join is (I assume you know that). 它们是方括号,它们表示一个数组, $join是(我想你知道)。 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. 切记,当您创建一个JSON中不存在的关联数组时,它会将其转换为对象。

As long as your arrays have different keys, you can also use array_merge or add the two arrays together ( $array1 + $array2 ). 只要您的数组具有不同的键,您还可以使用array_merge或将两个数组加在一起( $array1 + $array2 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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