Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7792201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:06:59+00:00 2026-06-01T22:06:59+00:00

I have 2 sets of code, which both work on their own, but I

  • 0

I have 2 sets of code, which both work on their own, but I need to combine them both, and havent been able to do it.

The end result should be to display a text file, line by line, with array #, alphabetically (and therefore array numbers should display at the end of each line).

FIRST PIECE OF CODE

<?php
$filename="users.txt"; 
$lines = array();
$file = fopen($filename, "r");

while(!feof($file)) { 
    $lines[] = fgets($file,4096);
} 

natcasesort($lines);
$text = implode("<br />", $lines);
print_r($text);

fclose ($file); 
?>

SECOND PIECE OF CODE

<?php 
 $lines = file('users.txt'); 
 foreach ($lines as $line_num => $line) 
{ 
 print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; 
 }
 ?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:07:00+00:00Added an answer on June 1, 2026 at 10:07 pm

    There are two easy ways to do this. This one will only work for PHP >= 5.4.0:

    $lines = file('users.txt');
    asort($lines, SORT_NATURAL | SORT_FLAG_CASE); // natcasesort that preserves keys!
    
    foreach ($lines as $num => $line) { 
        printf("%s (line #%d)<br>", $line, $num);
    }
    

    If using an earlier version, you can achieve the same effect with uasort and strnatcasecmp:

    $lines = file('users.txt');
    uasort($lines, 'strnatcasecmp'); // natcasesort that preserves keys
    
    // the loop is the same
    

    See it in action.

    In both case the idea is to use a sort function that maintains index association, so that after the lines are sorted they remain indexed by their original key (which is the line number in the file). Both usort and uasort do this; uasort allows you to specify the sort comparison function yourself while usort only allows a few built-in options (which do not include natural sorting in PHP < 5.4).

    As an aside: please do not use the <font> tag ever again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have code which sets up a basic client to server scenario and
I need help with my C code. I have a function that sets a
I have sets of 5, 6 and 7 digit numbers. I need them to
I have the following code which sets up some custom form elements on the
I have the need to merge data sets by row but they have differing
I have some code that sets up a dictionary with some defualt values for
I have some code that sets the lang var to the site default language.
I have a code that sets a new repeating alarm (on production I'll use
I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie
I have code in global.asax that sets some things in cache with CacheItemRemovedCallback. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.