PHP Sorting Arrays


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The pos() function returns the value of the current element in an array, and since no operation has been done, the current element is the first element.


  1. What will be the output of the following PHP code ?
    <?php
    $num = range(0, 6);
    print_r ($num);
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The range() function creates an array containing a range of elements.



  1. What will be the output of the following PHP code?
    <?php
    $City1 = array ("Noida", "Delhi", "Chennai", "Greenville", "Branford");
    $City2 = array ("Chennai", "Derby", "Enfield", "Delhi", "Greenville");
    $inter = array_intersect ($City1, $City2);
    print_r ($inter);
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    The array_intersect() function returns a key preserved array consisting only of those values present in the first array that are also present in each of the other input arrays.


  1. What will be the output of the following PHP code ?
    <?php
    $Str = array("Ajit" => "23", "Ats" => "21", "Aju" => "22");
    ksort($Str);
    foreach($Str as $s => $s_value)
    {
    echo "Key=" . $s . ", Value=" . $s_value;
    echo "\n";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    The ksort() function sorts an associative array in ascending order, according to the key.



  1. What will be the output of the following PHP code?
    <?php
    $Name = array ("Neha", "Ajit", "Rahul", "Ats");
    $Name = array_flip($Name);
    echo ($Name[0]);
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    As we are flipping the values, $Name [“Neha”] = 0, $Name [“Ajit”] = 1 and so on.