我有一个包含 60 个元素的数组,需要对其进行排序(从低到高),并且必须将该排序后的数组的前 10 个元素作为另一个数组。我被困住了。任何帮助都会很棒!
到目前为止,我有 -
$lists_store = get_stores($user_location);
asort($lists_store); // this give me sorted array as expected
echo "<pre>";
print_r($lists_store);
exit;
将输出..
Array
(
[39] => 6291
[52] => 6293
[63] => 6322
[64] => 6323
[46] => 6327
[37] => 6338
[26] => 6341
[44] => 6346
[20] => 6346
[17] => 6346
[11] => 6349
[43] => 6349
[24] => 6350
[21] => 6351
[12] => 6351
[10] => 6352
[27] => 6354
[22] => 6354
[19] => 6355 .....
现在问题来了..
$lists_store = array_slice($lists_store, 0, 10); // gives me first 10 elements of array but on the key basis
echo "<pre>";
print_r($lists_store);
exit;
将输出..
Array
(
[0] => 6291
[1] => 6293
[2] => 6322
[3] => 6323
[4] => 6327
[5] => 6338
[6] => 6341
[7] => 6346
[8] => 6346
[9] => 6346
)
期望的输出——
Array
(
[39] => 6291
[52] => 6293
[63] => 6322
[64] => 6323
[46] => 6327
[37] => 6338
[26] => 6341
[44] => 6346
[20] => 6346
[17] => 6346
)