0

I have a huge issue, I cant find any way to sort array entries. My code:

<?php
error_reporting(0);
$lines=array();
$fp=fopen('file.txt, 'r');
$i=0;
while (!feof($fp))
{
    $line=fgets($fp);
    $line=trim($line);
    $lines[]=$line;
    $oneline = explode("|", $line);
    if($i>30){
    $fz=fopen('users.txt', 'r');
    while (!feof($fz))
{
    $linez=fgets($fz);
    $linez=trim($linez);
    $lineza[]=$linez;
    $onematch = explode(",", $linez);
    if (strpos($oneline[1], $onematch[1])){
        echo $onematch[0],$oneline[4],'<br>';
    }
    else{
    }
    rewind($onematch);
}
    }
    $i++;
}
fclose($fp);
?>

The thing is, I want to sort items that are being echo'ed by $oneline[4]. I tried several other posts from stackoverflow - But was not been able to find a solution.

5
  • How do you want to sort them? Commented Jan 20, 2016 at 22:57
  • maybe you can try with save the data that return from your fopen() and save maybe on a json or array and then you can order this new array or json (local store ) Commented Jan 20, 2016 at 22:57
  • @kittykittybangbang I want to sort it in Descending order. Commented Jan 20, 2016 at 23:14
  • 2
    You can't sort an array while iterating through it. You need to load your data into an array, then use sort() or asort() or whichever sort method you want. Once complete you can loop through the array and do your outputs. Commented Jan 20, 2016 at 23:15
  • Can you provide an example of what a single $line would contain? As it seems to be a string which (perhaps, need to see a sample to be sure) could be sorted easily if it can be turned into an array (and back to string, after the sort is done). Commented Jan 20, 2016 at 23:37

1 Answer 1

2

The anser to your question is that in order to sort $oneline[4], which seems to contain a string value, you need to apply the following steps:

  1. split the string into an array ($oneline[4] = explode(',', $oneline[4]))
  2. sort the resulting array (sort($oneline[4]))
  3. combine the array into a string ($oneline[4] = implode(',', $oneline[4]))

As I got the impression variable naming is low on the list of priorities I'm re-using the $oneline[4] variable. Mostly to clarify which part of the code I am referring to.


That being said, there are other improvements you should be making, if you want to be on speaking terms with your future self (in case you need to work on this code in a couple of months)

  • Choose a single coding style and stick to it, the original code looked like it was copy/pasted from at least 4 different sources (mostly inconsistent quote-marks and curly braces)
  • Try to limit repeating costly operations, such as opening files whenever you can (to be fair, the agents.data could contain 31 lines and the users.txt would be opened only once resulting in me looking like a fool)

I have updated your code sample to try to show what I mean by the points above.

<?php
error_reporting(0);
$lines = array();
$users = false;

$fp = fopen('http://20.19.202.221/exports/agents.data', 'r');
while ($fp && !feof($fp)) {
    $line = trim(fgets($fp));
    $lines[] = $line;
    $oneline = explode('|', $line);

    //  if we have $users (starts as false, is turned into an array
    //  inside this if-block) or if we have collected 30 or more 
    //  lines (this condition is only checked while $users = false)
    if ($users || count($lines) > 30) {
        //  your code sample implies the users.txt to be small enough
        //  to process several times consider using some form of 
        //  caching like this
        if (!$users) {
            //  always initialize what you intend to use
            $users = [];

            $fz = fopen('users.txt', 'r');
            while ($fz && !feof($fz)) {
                $users[] = explode(',', trim(fgets($fz)));
            }
            //  always close whatever you open.
            fclose($fz);
        }

        //  walk through $users, which contains the exploded contents 
        //  of each line in users.txt
        foreach ($users as $onematch) {
            if (strpos($oneline[1], $onematch[1])) {
                //  now, the actual question: how to sort $oneline[4]
                //  as the requested example was not available at the
                //  time of writing, I assume
                //  it to be a string like: 'b,d,c,a'

                //  first, explode it into an array
                $oneline[4] = explode(',', $oneline[4]);
                //  now sort it using the sort function of your liking
                sort($oneline[4]);
                //  and implode the sorted array back into a string
                $oneline[4] = implode(',', $oneline[4]);

                echo $onematch[0], $oneline[4], '<br>';
            }
        }
    }
}
fclose($fp);

I hope this doesn't offend you too much, just trying to help and not just providing the solution to the question at hand.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.