我有一个带有属性坐标的对象arraylist。
我将如何展平该阵列。通过展平,我的意思是保持坐标数组,但是附加数组中的信息应该在最后的第一个数组内推送。
"checksum": "b29e57b048be1ff004de37679c6f6ea4",
"type": "LineString",
"color": "#0000ff",
"text": "",
"count": 2,
"length": 1,
"coordinates": [
[
[
1245613.4845831,
8456782.8389739
],
[
1245612.5888367,
8456786.5712507
],
[
1245611.3945081,
8456792.6921846
],
[
1245611.9916724,
8456795.678006
]
],
[
[
1245560.9341261,
8456793.1400578
],
[
1245561.6805815,
8456795.528715
],
[
1245562.2777458,
8456797.9173721
],
[
1245564.3678208,
8456797.9173721
],
[
1245565.7114404,
8456802.8439774
]
],
[
[
1245560.9341261,
8456793.1400578
],
[
1245561.6805815,
8456795.528715
],
[
1245562.2777458,
8456797.9173721
],
[
1245564.3678208,
8456797.9173721
],
[
1245565.7114404,
8456802.8439774
]
]
]
我希望坐标数组排序:
"coordinates": [
[
[
1245613.4845831,
8456782.8389739
],
[
1245612.5888367,
8456786.5712507
],
[
1245611.3945081,
8456792.6921846
],
[
1245611.9916724,
8456795.678006
],
[
1245560.9341261,
8456793.1400578
],
[
1245561.6805815,
8456795.528715
],
[
1245562.2777458,
8456797.9173721
],
[
1245564.3678208,
8456797.9173721
],
[
1245565.7114404,
8456802.8439774
],
[
1245560.9341261,
8456793.1400578
],
[
1245561.6805815,
8456795.528715
],
[
1245562.2777458,
8456797.9173721
],
[
1245564.3678208,
8456797.9173721
],
[
1245565.7114404,
8456802.8439774
]
]
答案 0 :(得分:1)
您可以遍历嵌套值并将所有这些值合并到一个新数组中:
在这里,$old
是最初的coordinates
嵌套数组。
$new = array();
foreach($old as $oldNest){
$new = array_merge($new, $oldNest);
}
答案 1 :(得分:1)
简单地迭代坐标以构建扁平化数组:
$coordinatesOnly = $original['coordinates'];
$flattenedCoordinates = [];
foreach ($coordinatesOnly as $coordinatesSet) {
$flattenedCoordinates = array_merge($flattenedCoordinates, $coordinatesSet);
}
var_dump($flattenedCoordinates);
鉴于你原来的阵列:
$original = [
'checksum' => 'b29e57b048be1ff004de37679c6f6ea4',
'type' => 'LineString',
'color' => '#0000ff',
'text' => '',
'count' => 2,
'length' => 1,
'coordinates' => [
[
[
1245613.4845831,
8456782.8389739
],
[
1245612.5888367,
8456786.5712507
],
[
1245611.3945081,
8456792.6921846
],
[
1245611.9916724,
8456795.678006
]
],
[
[
1245560.9341261,
8456793.1400578
],
[
1245561.6805815,
8456795.528715
],
[
1245562.2777458,
8456797.9173721
],
[
1245564.3678208,
8456797.9173721
],
[
1245565.7114404,
8456802.8439774
] ],
[
[
1245560.9341261,
8456793.1400578
],
[
1245561.6805815,
8456795.528715
],
[
1245562.2777458,
8456797.9173721
],
[
1245564.3678208,
8456797.9173721
],
[
1245565.7114404,
8456802.8439774
]
]
]
];
你得到了你期望的东西:
array(14) {
[0]=>
array(2) {
[0]=>
float(1245613.4845831)
[1]=>
float(8456782.8389739)
}
[1]=>
array(2) {
[0]=>
float(1245612.5888367)
[1]=>
float(8456786.5712507)
}
[2]=>
array(2) {
[0]=>
float(1245611.3945081)
[1]=>
float(8456792.6921846)
}
[3]=>
array(2) {
[0]=>
float(1245611.9916724)
[1]=>
float(8456795.678006)
}
[4]=>
array(2) {
[0]=>
float(1245560.9341261)
[1]=>
float(8456793.1400578)
}
[5]=>
array(2) {
[0]=>
float(1245561.6805815)
[1]=>
float(8456795.528715)
}
[6]=>
array(2) {
[0]=>
float(1245562.2777458)
[1]=>
float(8456797.9173721)
}
[7]=>
array(2) {
[0]=>
float(1245564.3678208)
[1]=>
float(8456797.9173721)
}
[8]=>
array(2) {
[0]=>
float(1245565.7114404)
[1]=>
float(8456802.8439774)
}
[9]=>
array(2) {
[0]=>
float(1245560.9341261)
[1]=>
float(8456793.1400578)
}
[10]=>
array(2) {
[0]=>
float(1245561.6805815)
[1]=>
float(8456795.528715)
}
[11]=>
array(2) {
[0]=>
float(1245562.2777458)
[1]=>
float(8456797.9173721)
}
[12]=>
array(2) {
[0]=>
float(1245564.3678208)
[1]=>
float(8456797.9173721)
}
[13]=>
array(2) {
[0]=>
float(1245565.7114404)
[1]=>
float(8456802.8439774)
}
}