PHP Sorting Arrays


  1. Which of the functions is used to sort an array in descending order?











  1. 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.


  1. 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";
    }
    ?>











  1. 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.



  1. What will be the output of the following PHP code?
    <?php
    $str = array ("Rahul", "Ajit", "Aju", "Ats");
    sort($str);
    print_r($str);
    ?>











  1. 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.


  1. Which of the following function should be used, to sort the array in case?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    natcasesort()



  1. 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);
    ?>











  1. 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.