PHP Sorting Arrays
- Which of the functions is used to sort an array in descending order?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
sort() function is used to sort in ascending order whereas rsort() meaning reverse sort is used for sorting in descending order.
- What will be the output of the following PHP code?
<?php
$subject = array('a' => 'Math', 'b' => 'Science', 'c'=> 'Geography');
asort($subject);
foreach ($subject as $key => $val)
{
echo "$key = $val";
echo "\n";
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
The function asort() sorts the array in ascending order, except that the key/value corresponding is maintained.
- What will be the output of the following PHP code?
<?php
$str = array ("Rahul", "Ajit", "Aju", "Ats");
sort($str);
print_r($str);
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
While sorting each character is compared with the others and sorted using ascii values therefore we the sorted array to be like option c.
- Which of the following function should be used, to sort the array in case?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
natcasesort()
- What will be the output of the following PHP code?
<?php
$num = array ("2","3","4", "5", "6", "7", "8", "9", "10");
$face = array ("A", "J", "Q", "K");
$cards = array_merge ($face, $num);
print_r ($cards);
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
The resulting array will begin with the first input array parameter, appending each subsequent array parameter in the order of appearance.