我有一個數組,其中包含60個元素,需要對它進行排序(從低到高)並且必須獲得前10個該排序數組的元素作爲另一個數組。我被卡住了。任何幫助將是偉大的!根據數值對數組進行排序,並獲得前n個元素作爲排序數組的其他數組
到目前爲止,我有 -
$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
)
'list_store = array_chunk($ lists_store,10,true);' – bansi
你讀過['array_chunk'](http://php.net/array_chunk)的*文檔*嗎? – deceze
我更新了我的問題,我正在使用array_slice,並且我在這裏提到的結果也是array_slice – codepixlabs