How to sort an array from a file (PHP)?

I have a file here that contains a list of database names and the corresponding size. Now I want to sort the size from largest to lowest, and the database name should go along it when showing. Using PHP here. Can someone help me?

Here is a simple code for this:

$file_name = test.txt
$handle = @fopen($file_name, "r");

if ($handle) {
    while (!feof($handle)) {

    $buffer = fgets($handle, 4096);
        $data = explode(" ",$buffer);
       echo $data[1]."\n";
    }
    fclose($handle);
}

The file is as follows:

DatabaseName 300 KB 

Note: $ data [1] contains dimensions. Should I place it on an array? what about db name?

The answers are much appreciated. =)

+3
source share
4 answers

First you need to build an array with an element that you can sort, and then use usort or similarly perform sorting based on your user criteria.

//first build up an array of databases with a unified size in bytes, ensuring
//we account for those postfixes like KB,MB,and GB
$databases=array();
while (!feof($handle)) {

    $buffer = fgets($handle, 4096);
    $data = explode(" ",$buffer);
    if (count($data)==3)
    {
        $size=$data[1];
        switch ($data[2])
        {
            case 'KB': $size*=1024; break;
            case 'MB': $size*=1024*1024; break;
            case 'GB': $size*=1024*1024*1024; break;
        }

        $data[3]=$size;
        $databases[]=$data; 
    }
    else
    {
        die("Bad line in file: $buffer");
    }
}

:

function cmp($a, $b)
{
    if ($a[3] == $b[3]) {
        return 0;
    }
    return ($a[3] < $b[3]) ? 1 : -1;
}

usort($databases, "cmp");
+2

file() PHP.

:

DatabaseName 300 KB
DatabaseName 300 KB
DatabaseName 300 KB
DatabaseName 300 KB

, PHP .

$data = file('myfile.txt');

foreach($data as $one_line)
{
    $db[] = explode(" ",$one_line)
    //will have $db[0][0] = 'dbname';
    //will have $db[0][2] = '30';
    //will have $db[0][2] = 'KB';   
    //will have $db[1][0] = 'dbname';
    //will have $db[1][3] = '30';
    //will have $db[1][2] = 'KB';
}
array_multisort($db[1], SORT_NUMERIC, SORT_DESC);

, : http://fr2.php.net/manual/en/function.array-multisort.php#79491

, , , .

+2
function databases_sort($a, $b) {
    if ($a[1] == $b[1]) { return 0; }

    return ($a[1] < $b[1]) ? -1 : 1;
}

$file_name = "test.txt";
$handle = @fopen($file_name, "r");

$databases = array();

if ($handle) { 
    while (!feof($handle)) {

        $buffer = fgets($handle, 4096);
        $data = explode(" ",$buffer);
        $databases[] = $data;
    //echo $data[1]."\n";
    }

    usort($databases, "databases_sort");

    foreach ($databases as $d) {
        echo $d[1];
    }

    fclose($handle);
}

, , 1 . ( ) .

. , usort documentation, - , .

: d'oh, : -)

+1
source

Have you considered parsing the binary structure of the search tree instead of an array? That way, you could sort the data by traversing the tree, and you could quickly find it.

0
source

Source: https://habr.com/ru/post/1714591/


All Articles