简体   繁体   English

WHILE循环内的PHP数组排序

[英]PHP Array sorting within WHILE loop

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]. 问题是,我想对$ oneline [4]回显的项目进行排序。 I tried several other posts from stackoverflow - But was not been able to find a solution. 我尝试了stackoverflow的其他几篇文章-但无法找到解决方案。

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: 您的问题的答案是,为了对似乎包含字符串值的$oneline[4]进行排序,您需要执行以下步骤:

  1. split the string into an array ( $oneline[4] = explode(',', $oneline[4]) ) 将字符串拆分成一个数组( $oneline[4] = explode(',', $oneline[4])
  2. sort the resulting array ( sort($oneline[4]) ) 对结果数组进行sort($oneline[4])sort($oneline[4])
  3. combine the array into a string ( $oneline[4] = implode(',', $oneline[4]) ) 将数组合并成一个字符串( $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. 当我得到印象变量的命名在优先级列表中很低时,我正在重新使用$oneline[4]变量。 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) 选择一个编码样式并坚持下去,原始代码看起来就像是从至少4个不同的源中复制/粘贴的(大多是引号和花括号不一致)
  • 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) 尝试限制重复进行的昂贵操作,例如在可能的情况下打开文件(为公平起见, agents.data可能包含31行,而users.txt只会打开一次,导致我看上去很傻)

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. 我希望这不会对您造成太大的伤害,只是尝试提供帮助,而不仅仅是提供解决当前问题的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM