WHILE 循环中的 PHP 数组排序


PHP Array sorting within WHILE loop

我有一个大问题,我找不到任何对数组条目进行排序的方法。我的代码:

<?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);
?>

问题是,我想对被 $oneline[4] 回显的项目进行排序。我尝试了来自stackoverflow的其他几个帖子 - 但无法找到解决方案。

您的问题是,为了对似乎包含字符串值的$oneline[4]进行排序,您需要应用以下步骤:

  1. 将字符串拆分为数组 ( $oneline[4] = explode(',', $oneline[4])
  2. 对生成的数组进行排序 ( sort($oneline[4])
  3. 将数组合并为字符串 ( $oneline[4] = implode(',', $oneline[4])

正如我得到的印象,变量命名在优先级列表中很低,我正在重用$oneline[4]变量。主要是为了澄清我指的是代码的哪一部分。


话虽如此,如果你想与未来的自己交谈,你应该做其他改进(以防你需要在几个月内处理这段代码)

  • 选择一种编码风格并坚持下去,原始代码看起来像是从至少 4 个不同的来源复制/粘贴的(主要是不一致的引号和大括号)
  • 尝试限制重复的昂贵操作,例如尽可能打开文件(公平地说,agents.data可能包含 31 行,users.txt只会打开一次,导致我看起来像个傻瓜)

我已经更新了您的代码示例,以尝试显示我所说的上述要点的含义。

<?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);

我希望这不会冒犯你太多,只是试图提供帮助,而不仅仅是为手头的问题提供解决方案。