对 3 行二维数组进行多重排序;对第一列升序排序,但最后使用空值,然后对其他列进行排序
我有几个像这样的数组。
Array
(
[state] => Array
(
[0] => WA
[1] => CA
[2] => CA
[3] => NV
[4] => MO
[5] => CA
[6] => CA
[7] => CA
[8] => CT
[9] => FL
[10] => FL
[11] => ID
[12] => ID
[13] => IN
[14] => MN
[15] => MN
[16] => NE
[17] => NY
[18] => TX
[19] => TX
[20] => WI
)
[counties] => Array
(
[0] => King, Snohomish
[1] => Contra Costa
[2] => Los Angeles
[3] => Clark
[4] => Jackson
[5] => Tulare
[6] => Sacramento
[7] => Riverside
[8] => New Haven
[9] => Pinellas
[10] => Lake
[11] => Canyon
[12] => Ada
[13] => Tippecanoe, White, Carroll
[14] => Crow Wing, Cass
[15] => Blue Earth
[16] => Douglas
[17] => Rockland
[18] => Webb
[19] => Harris
[20] => Waukesha, Milwaukee, Washington
)
[zipcodes] => Array
(
[0] => 98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083
[1] => 94530, 94804, 94805, 94803, 94806, 94564, 94547
[2] => 91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350
[3] => 89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112
[4] => 64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
)
)
我试图按邮政编码排序,然后按州排序,然后按县排序。我遇到的问题是让邮政编码按字符串中第一个邮政编码的升序显示。所以顺序是
64055, 64056, ...
89002, 89009, ...
91381, 91384, ...
94530, 94804, ...
这是我当前的结果。
WA,TL-WA-150,150,King, Snohomish,98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083
CA,HD-CA-125,125,Contra Costa,94530, 94804, 94805, 94803, 94806, 94564, 94547
CA,DH-CA-125,150,Los Angeles,91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350
NV,CM1-NV-150,150,Clark,89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112
MO,CJ-MO-150,150,Jackson,64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133
CA,GR6-CA-150,150,Tulare,
CA,SSJ-CA-150,150,Sacramento,
CA,LM1-CA-150,150,Riverside,
CT,TAMRM-CT-150,150,New Haven,
FL,GG-FL-150,150,Pinellas,
我的问题是如何让邮政编码按升序排序?
我现在正在用它来排序。
sortDataSet($difarray, 'zipcodes', SORT_DESC, SORT_NUMERIC, 'state', SORT_ASC, SORT_STRING,'counties', SORT_DESC, SORT_STRING);
function sortDataSet(&$dataSet) {
$args = func_get_args();
$callString = 'array_multisort(';
$usedColumns = array();
for($i = 1, $count = count($args); $i < $count; ++$i) {
switch(gettype($args[$i])) {
case 'string':
$callString .= '$dataSet[\''.$args[$i].'\'], ';
array_push($usedColumns, $args[$i]);
break;
case 'integer':
$callString .= $args[$i].', ';
break;
default:
throw new Exception('expected string or integer, given '.gettype($args[$i]));
}
}
foreach($dataSet as $column => $array) {
if(in_array($column, $usedColumns)) continue;
$callString .= '$dataSet[\''.$column.'\'], ';
}
eval(substr($callString, 0, -2).');');
}
I have an a couple of arrays like so.
Array
(
[state] => Array
(
[0] => WA
[1] => CA
[2] => CA
[3] => NV
[4] => MO
[5] => CA
[6] => CA
[7] => CA
[8] => CT
[9] => FL
[10] => FL
[11] => ID
[12] => ID
[13] => IN
[14] => MN
[15] => MN
[16] => NE
[17] => NY
[18] => TX
[19] => TX
[20] => WI
)
[counties] => Array
(
[0] => King, Snohomish
[1] => Contra Costa
[2] => Los Angeles
[3] => Clark
[4] => Jackson
[5] => Tulare
[6] => Sacramento
[7] => Riverside
[8] => New Haven
[9] => Pinellas
[10] => Lake
[11] => Canyon
[12] => Ada
[13] => Tippecanoe, White, Carroll
[14] => Crow Wing, Cass
[15] => Blue Earth
[16] => Douglas
[17] => Rockland
[18] => Webb
[19] => Harris
[20] => Waukesha, Milwaukee, Washington
)
[zipcodes] => Array
(
[0] => 98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083
[1] => 94530, 94804, 94805, 94803, 94806, 94564, 94547
[2] => 91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350
[3] => 89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112
[4] => 64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
)
)
I am trying to sort by zipcodes then by state then by counties. The problem I am having is getting the zipcodes to display in ascending order by the first zip in the string. So the order would be
64055, 64056, ...
89002, 89009, ...
91381, 91384, ...
94530, 94804, ...
Here are my current results.
WA,TL-WA-150,150,King, Snohomish,98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083
CA,HD-CA-125,125,Contra Costa,94530, 94804, 94805, 94803, 94806, 94564, 94547
CA,DH-CA-125,150,Los Angeles,91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350
NV,CM1-NV-150,150,Clark,89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112
MO,CJ-MO-150,150,Jackson,64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133
CA,GR6-CA-150,150,Tulare,
CA,SSJ-CA-150,150,Sacramento,
CA,LM1-CA-150,150,Riverside,
CT,TAMRM-CT-150,150,New Haven,
FL,GG-FL-150,150,Pinellas,
My question is how can I get the zipcodes to sort ascending?
I am using this for sorting right now.
sortDataSet($difarray, 'zipcodes', SORT_DESC, SORT_NUMERIC, 'state', SORT_ASC, SORT_STRING,'counties', SORT_DESC, SORT_STRING);
function sortDataSet(&$dataSet) {
$args = func_get_args();
$callString = 'array_multisort(';
$usedColumns = array();
for($i = 1, $count = count($args); $i < $count; ++$i) {
switch(gettype($args[$i])) {
case 'string':
$callString .= '$dataSet[\''.$args[$i].'\'], ';
array_push($usedColumns, $args[$i]);
break;
case 'integer':
$callString .= $args[$i].', ';
break;
default:
throw new Exception('expected string or integer, given '.gettype($args[$i]));
}
}
foreach($dataSet as $column => $array) {
if(in_array($column, $usedColumns)) continue;
$callString .= '$dataSet[\''.$column.'\'], ';
}
eval(substr($callString, 0, -2).');');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我假设您有一个包含三个数组的数组,即州、县和邮政编码。
我还假设包含这些数组的数组称为
$data
,因此请替换其中的数组名称。我相信你的原始阵列是这样设置的。
所以,这是我的解决方案:
我认为这就是您正在寻找的:
So I am assuming that you have an array with three arrays, of states, counties, and zipcodes.
I also assume that your array containing these arrays is called
$data
, so substitute the name of your array there.I believe your original array is setup like this.
So, this is my solution:
And I think this is what you area looking for:
创建一个临时数组,其中包含每个邮政编码列表中最低的邮政编码;当为空字符串时,分配PHP的最大整数。稍后将用于按升序排序。
创建该临时数组是在对逗号分隔的邮政编码值进行迭代排序时明智地执行的。在通过引用迭代邮政编码时,只需爆炸、排序,然后再次内爆。
最后调用
array_multisort()
,首先使用最低邮政编码的数组;这将影响所有后续可修改的数组元素(在本例中为所有剩余参数)。然后按邮政编码排序,然后按州排序,然后按县排序。代码:(演示)
Create a temporary array containing the lowest zipcode from each list of zipcodes; when an empty string, assign PHP's maximum integer. This will later be used to sort in an ascending direction.
Creating that temporary array is sensibly performed while doing the iterated sorting of comma-separated zipcode values. While iterating the zipcodes by reference, just explode, sort, then implode again.
Finally call
array_multisort()
with the array of lowest zipcodes first; this will affect all subsequent array elements which are modifiable (all of the remaining arguments in this case). Then sort by zipcodes, then state, then counties.Code: (Demo)