所以,我有以下数组
array(5) {
[0]=>
array(4) {
["name"]=>
string(30) "NAME 1"
["lat"]=>
string(8) "1.326738"
["lng"]=>
string(10) "102.746780"
}
[1]=>
array(4) {
["name"]=>
string(51) "NAME 2"
["lat"]=>
string(8) "1.324738"
["lng"]=>
string(10) "103.746780"
}
[2]=>
array(4) {
["name"]=>
string(29) "NAME 3"
["lat"]=>
string(8) "1.326738"
["lng"]=>
string(10) "102.746780"
}
[3]=>
array(4) {
["name"]=>
string(22) "NAME 4"
["lat"]=>
string(8) "1.324738"
["lng"]=>
string(10) "103.746780"
}
[4]=>
array(4) {
["name"]=>
string(38) "NAME 5"
["lat"]=>
string(8) "1.334755"
["lng"]=>
string(10) "102.746780"
}
}
如何通过lat和lng字段对数组进行分组?大多数已经在SO上回答的问题只是按一个字段分组。我想输出变得有点像这样
array(5) {
[0]=>
array(4) {
["name"]=>
string(38) "NAME 5"
["lat"]=>
string(8) "1.334755"
["lng"]=>
string(10) "102.746780"
}
["group1"]=>
array(2) {
[0]=>
array(4) {
["name"]=>
string(30) "NAME 1"
["lat"]=>
string(8) "1.326738"
["lng"]=>
string(10) "102.746780"
}
[1]=>
array(4) {
["name"]=>
string(29) "NAME 3"
["lat"]=>
string(8) "1.326738"
["lng"]=>
string(10) "102.746780"
}
}
["group2"]=>
array(2) {
[0]=>
array(4) {
["name"]=>
string(29) "NAME 3"
["lat"]=>
string(8) "1.324738"
["lng"]=>
string(10) "103.746780"
}
[1]=>
array(4) {
["name"]=>
string(51) "NAME 2"
["lat"]=>
string(8) "1.324738"
["lng"]=>
string(10) "103.746780"
}
}
}
我怎样才能做到这一点?
答案 0 :(得分:1)
您可以使用group by(lat,lng)和此功能。
groupBy('lat', 'lng', $data);
function groupBy($keyA, $keyB, $data)
{
$results = [];
foreach ($data as $key => $value) {
$a = $value[$keyA];
$b = $value[$keyB];
$results[$a . '-' . $b][] = $value;
}
// If you want to set key is group1, group2, etc..
$groupNo = 1;
$temp = [];
foreach ($results as $key => $value) {
if ( count($value) > 1)
{
$temp['group'. $groupNo] = $value;
$groupNo = $groupNo + 1;
}
else
{
$temp[] = $value[0];
}
}
$results = $temp;
return $results;
}
答案 1 :(得分:0)
<?php
$aEntry = array();
$aEntry[ 'name' ] = 'NAME 1';
$aEntry[ 'lat' ] = '1.326738';
$aEntry[ 'lng' ] = '102.746780';
$aEntry2 = array();
$aEntry2[ 'name' ] = 'NAME 2';
$aEntry2[ 'lat' ] = '1.326738';
$aEntry2[ 'lng' ] = '102.746780';
$aEntry3 = array();
$aEntry3[ 'name' ] = 'NAME 2';
$aEntry3[ 'lat' ] = '1.326738';
$aEntry3[ 'lng' ] = '102.746780';
$aEntry4 = array();
$aEntry4[ 'name' ] = 'NAME 3';
$aEntry4[ 'lat' ] = '1.326731';
$aEntry4[ 'lng' ] = '102.746781';
$aEntry5 = array();
$aEntry5[ 'name' ] = 'NAME 4';
$aEntry5[ 'lat' ] = '1.326731';
$aEntry5[ 'lng' ] = '102.746781';
$aEntry6 = array();
$aEntry6[ 'name' ] = 'NAME 5';
$aEntry6[ 'lat' ] = '1.326731';
$aEntry6[ 'lng' ] = '102.746781';
$aEntry7 = array();
$aEntry7[ 'name' ] = 'NAME 5';
$aEntry7[ 'lat' ] = '1.11111';
$aEntry7[ 'lng' ] = '102.11111';
$aData = array();
$aData[] = $aEntry;
$aData[] = $aEntry2;
$aData[] = $aEntry3;
$aData[] = $aEntry4;
$aData[] = $aEntry5;
$aData[] = $aEntry6;
$aData[] = $aEntry7;
$aFormatted = array();
$iCountData = count( $aData );
for( $i = 0; $i < $iCountData; ++$i )
{
$vKeyToAppendTo = '';
foreach( $aFormatted as $sKey => $aValues )
{
// Check if we already have the lat/lng combination stored.
// Each formatted entry will share lat/lng
if( ( $aValues[ 0 ][ 'lat' ] == $aData[ $i ][ 'lat' ] ) && ( $aValues[ 0 ][ 'lng' ] == $aData[ $i ][ 'lng' ] ) )
{
// Set the array key to append to.
$vKeyToAppendTo = $sKey;
break;
}
}
// If we have a key match append.
if( !empty( $vKeyToAppendTo ) )
{
$aFormatted[ $vKeyToAppendTo ][] = $aData[ $i ];
}
// Otherwise create the new lat/lng entry group.
else
{
$aFormatted[ 'group' . $i ][] = $aData[ $i ];
}
}
var_dump( $aFormatted );
?>