PHP Array Sorting
In this page:
sort($array); // ascending values, reindexed
rsort($array); // descending
asort($array); // by value, keep keys
ksort($array); // by key
arsort($array);
krsort($array);
Indexed Arrays Sort करना
sort() किसी indexed array की values को ascending order में reorder करता है और keys को 0 से re-index करता है, जो एक simple list के लिए वही है जो आप चाहते हैं लेकिन किसी associative array में किसी भी meaningful string keys को destroy कर देगा।
उदाहरण: Sorting Indexed Arrays
<?php
// Declare `$numbers` as an array: `[3, 1, 4, 1, 5]`
$numbers = [3, 1, 4, 1, 5];
// Call `sort($numbers)`
sort($numbers);
// Print a human-readable dump of `$numbers`
print_r($numbers);
?>
Login to try C/C++/Java/PHP code in the editor
Value से Associative Arrays Sort करना
asort() और ksort() sort करते समय key-to-value associations preserve करते हैं: asort() value से sort करता है, ksort() key से sort करता है, दोनों original pairing को intact रखते हुए — तब ज़रूरी जब keys मायने रखती हों, जैसे usernames को scores से mapped।
उदाहरण: Sorting Associative Arrays by Value
<?php
// Declare `$scores` as an array: `["alice" => 90, "bob" => 75]`
$scores = ["alice" => 90, "bob" => 75];
// Call `asort($scores)`
asort($scores);
// Print a human-readable dump of `$scores`
print_r($scores);
// Call `ksort($scores)`
ksort($scores);
// Print a human-readable dump of `$scores`
print_r($scores);
?>
Login to try C/C++/Java/PHP code in the editor
Key से Associative Arrays Sort करना
rsort(), arsort(), और krsort() क्रमशः sort(), asort(), और ksort() के descending-order counterparts हैं, किसी leaderboard पर सबसे पहले highest scores दिखाने जैसे tasks के लिए उपयोगी।
उदाहरण: Sorting Associative Arrays by Key
<?php
// Declare `$scores` as an array: `["alice" => 90, "bob" => 75, "carol" => 85]`
$scores = ["alice" => 90, "bob" => 75, "carol" => 85];
// Call `arsort($scores)`
arsort($scores);
// Print a human-readable dump of `$scores`
print_r($scores);
?>
Login to try C/C++/Java/PHP code in the editor
usort से Custom Sorting
usort() आपको अपना खुद का comparison function supply करने देता है, उन cases के लिए sort order पर पूरा control देते हुए जिन्हें built-in functions handle नहीं कर सकते, जैसे associative arrays के किसी array को एक specific field से sort करना।
उदाहरण: Custom Sorting with usort
<?php
// Declare `$people` as an array: `[["name" => "Bob", "age" => 25], ["name" => "Alice", "age" => 30]]`
$people = [["name" => "Bob", "age" => 25], ["name" => "Alice", "age" => 30]];
usort($people, function ($a, $b) {
// Return `$a["age"] <=> $b["age"]` from this function
return $a["age"] <=> $b["age"];
});
// Print a human-readable dump of `$people`
print_r($people);
?>
Login to try C/C++/Java/PHP code in the editor
Natural Order Sorting
PHP में sorting functions array को in place modify करते हैं और एक नई sorted array return करने के बजाय true/false return करते हैं, जो एक common gotcha है अगर आप उम्मीद करें कि $sorted = sort($array); काम करेगा — यह नहीं करता, sort($array) वह है जो आपको चाहिए।
उदाहरण: Natural Order Sorting
<?php
$numbers = [3, 1, 2];
sort($numbers); // modifies in place, returns true/false
print_r($numbers);
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि किसी associative array पर
sortइसकी keys रखेगा, जबकिsortउन्हें discard कर देता है;asortयाksortइस्तेमाल करें। sort($arr)को एक expression की तरह इस्तेमाल करना, जैसे$b = sort($a);, जबकि यह in place sort करता है और true return करता है।- एक
usortcallback लिखना जो negative, zero या positive number के बजाय एक boolean return करता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: