Hello! I'm new to the forum, but I'm really excited about being part of the community here.

So here's my question. I'm trying to order string based on a custom alphabet. I have php code that breaks the file to be sorted into an array of strings. I'm attempting to order each string. The strings are ordering themselves correctly, but they are moving themselves around in the array somehow. I'm new to php, so I'm hoping someone could show me my errors.

Thanks 🙂

<?php
echo " start ";
        $file = fopen('inFile.txt', "r");
        $firstLine = fgets($file);
echo " hi ";
$x = 0;
while(! feof($file)){
$restOfFile[$x] = fgets($file);
$x++;
}
$firstLine = str_replace(' ','',$firstLine);


fclose($file);
//echo $firstLine;
//print_r( $restOfFile);

function mylst($a,$b){
$pos1 = 0;
$pos2 = 0;
global $firstLine;


if(strlen($a) > strlen($b)){
     $string = $b;
}else{
     $string = $a;
}

for ($i=0;$i<(strlen($string) && $pos1===$pos2); $i++){
     $pos1 = strpos($firstLine,substr($a,1));
     $pos2 = strpos($firstLine,substr($b,1));
}

if ($pos1 === $pos2 && strlen($a) !== strlen($b)){
     return (strlen($a)-strlen($b));
}

  return $pos1-$pos2;
}


echo " BEFORE ";

print_r($restOfFile);

echo " AFTER ";
usort($restOfFile,"mylst");
print_r($restOfFile);


?>

    Please excuse my attempts at debugging 🙂 I erased some, but not all apparently

      I apologize, the strings are supposed to put themselves in alphabetical order, as well as the alphabetically in the array. however, it's not working right.

        zalas35;11044845 wrote:

        Hello! I'm new to the forum, but I'm really excited about being part of the community here.

        Welcome.

        zalas35;11044845 wrote:

        So here's my question. I'm trying to order string based on a custom alphabet.

        I hope you can understand that this statement does not clearly indicate what you are trying to do.

        zalas35;11044845 wrote:

        I have php code that breaks the file to be sorted into an array of strings.

        From what I can tell, your code extracts the first line ($firstLine) for use in your sorting function and then creates an array, $restOfFile, which contains the remainder of the file.

        zalas35;11044845 wrote:

        I'm attempting to order each string.

        This statement is unclear and to me does not reflect what is happening. Each string is not affected, but their sequence in the array is.

        zalas35;11044845 wrote:

        I'm attempting to order each string.

        The strings are ordering themselves correctly, but they are moving themselves around in the array somehow. I'm new to php, so I'm hoping someone could show me my errors.[/QUOTE]
        The [man]usort[/man] function takes an array of strings and rearranges the sequence of strings in that array based on a function you have defined. That's what it's designed to do.

        You need to express more clearly what is wrong.

          zalas35;11044849 wrote:

          I apologize, the strings are supposed to put themselves in alphabetical order, as well as the alphabetically in the array. however, it's not working right.

          I hope you realize this is hardly clear enough to tell what's wrong. You are going to have to be much more precise. Generally speaking "it's not working right" is never a helpful description of a problem. It's just too vague.

          Do you mean that you want to change the order of characters within each line? Do you want to alphabetize the characters in each line or do you want to split each line up into words and then alphabetize those words?

          Is there something wrong with the order that you obtain from the usort function? If so, what is the problem with this ordering?. "it's not working right" doesn't tell anyone what you don't like about the ordering you have managed to obtain.

            Write a Reply...