How To Use Krsort Array Function With Example In PHP

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

Sort Array elements in Descending  Order, According to Key

PHP krsort Function with example

First Need to Create Arrays

1.Create Array:

$subjects = array("Mathematics"=>"85", "Science"=>"70", "English"=>"92");

2.krsort()

Sort Array elements in Descending  Order, According to Key

Example(1)
<?php
$subjects = array("Mathematics"=>"85", "Science"=>"70", "English"=>"92");
krsort($subjects);

foreach($subjects as $x => $x_value) {
    echo "<h4> Key=" . $x . ", Value=" . $x_value ."</h4>";
}
?>


Complete Code of krsort() :
<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP Sorting Arrays</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>PHP Sorting Arrays</h1>
    </div>
    <h2>1.krsort()</h2>
    <h2>Sort Array elements in Descending  Order, According to Key </h2>
    <div class="well">
        <?php
        $subjects = array("Mathematics"=>"85", "Science"=>"70", "English"=>"92");
        krsort($subjects);

        foreach($subjects as $x => $x_value) {
            echo "<h4> Key=" . $x . ", Value=" . $x_value ."</h4>";
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

 

Related Post