Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Sort array

<?php

$array = array(4, 6, -2, 1, 3, 90, 0, 9, 8, 7, 5);
$newarray = array();
$x = 0;
$arraysize = sizeof($array);

echo "\nOld "; print_r($array); echo "\n";

// get an array to loop over all the numbers in the array, accounting for changes in length
foreach ($array as $not_needed) {
    // set our original lowest to infinity so that everything is smaller than it
    $lowest = INF;
    // loop through all the array members (again) to find the lowest
    foreach ($array as $arraymember){
        // if the current member is lower than the lowest one so far, make it the lowest
        if ($lowest > $arraymember){
            $lowest = $arraymember;
        } else {};
    }
    $key = array_search($lowest, $array);
    unset($array[$key]);
    $newarray[] += $lowest;
}

// echo the whole array
echo "\nNew "; print_r($newarray); echo "\n";

?>

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.