1

I have a .txt file that looks like this:

john 1000 
mike 8393 
tom 1000 
bob 233 
roger 2 
daniel 233 
... ...

I need to put every line into array and sort by number size without losing what name goes with what number.

Also some numbers are repeating through the file.

Finally I want to echo elements of an array sorted by number size.

0

3 Answers 3

1

You could break each line into an array of integers and strings (split your current strings on the space) and then sort the array using ksort (assuming the key is the integer) and there you go!

You can of course alternatively use a more robust sort, but this will get you there.

You can then print it by using print_r to print the human readable version of the array

http://php.net/manual/en/array.sorting.php

http://php.net/manual/en/function.print-r.php

Sign up to request clarification or add additional context in comments.

Comments

1

If you're running php 5.3+

$fileLines = explode("\n", $fileContents);
usort($fileLines, function($a, $b) { 
  $aNumber = (int)substr($a, strpos($a, ' ')+1);
  $bNumber = (int)substr($b, strpos($b, ' ')+1);
  if($aNumber === $bNumber) {
    return 0;
  }
  return $aNumber > $bNumber ? 1 : -1;
}); 

If you're running a lower version, convert the function into a global function and provide the name as a callback string.

usort($fileLines, 'sortlines'); 

function sortlines($a, $b) { 
  $aNumber = (int)substr($a, strpos($a, ' ')+1);
  $bNumber = (int)substr($b, strpos($b, ' ')+1);
  if($aNumber === $bNumber) {
    return 0;
  }
  return $aNumber > $bNumber ? 1 : -1;
}

then

var_dump($fileLines);

Comments

0

You can go three ways:

1.Create two-dimensional array with numeric indexes and sort it with usort(); and lambda function

$list = array(array('name' => 'john', 'number' => 1000), 
    array('name' => 'mike', 'number' => 8393), 
    array('name' => 'tom', 'number' => 1000)
);

$by = 'number';

usort($list, function($first, $second) use ($by)
    {
        if ($first[$by] > $second[$by] { return 1; }
        elseif (first[$by] < $second[$by]) { return -1; }
        return 0;
    }
);

2.Create array with indexes as names and sort it with sort();

$list = array('john' => 1000, 
    'mike' => 8393,
    'tom' => 1000
);

sort($list);

3.Create array with indexes as numbers and sort it with ksort();

$list = array(1000 => 'john', 
    8393 => 'mike',
    1000 => 'tom'
);

ksort($list);

If you choose first way you can address the element as

$list[0][name] = 'bob'
$list[1][number] = 1000;

Second

$list['john'] = 1001;

Third

$list[1000] = 'bob';

In last two ways you should use foreach to go through array

Use

print_r($list);

or var_dump($list);

to print the array, or create your own code

P.S. Don't forget thar calling usort with lambda function is PHP 5.3 way, if you use earlier version of PHP, you should use discrete function

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.