echo a file contents backwards.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

echo a file contents backwards.

Post by Sevengraff »

Ive seen the code in many tutorials, but now that i need it, i cant find it.
i want to use the code:

Code: Select all

$file_array = file("test.txt"); 
for ( $i = 0; $i < count($file_array); $i++ ) &#123; 
echo $file_array&#1111;$i]; 
&#125;
but i need it to do it in reverse order (so the last line is displayed first). what do i do?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

http://www.php.net/manual/en/function.array-reverse.php

Code: Select all

array_reverse()

Description
array array_reverse ( array array [, bool preserve_keys])


array_reverse() takes input array and returns a new array with the order of the elements reversed, preserving the keys if preserve_keys is TRUE.
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

ok... how would i use that in my code that i posted? because "array array_reverse ( array array [, bool preserve_keys])" don't help me at all.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Code: Select all

$file_array = file("test.txt");
$contents = "";
for ($i = 0; $i < count($file_array); $i++)
   $contents .= $file_array&#1111;$i];

echo strrev($contents);
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

Thanks!!!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sevengraff wrote:ok... how would i use that in my code that i posted? because "array array_reverse ( array array [, bool preserve_keys])" don't help me at all.
Yes but the link to the manual entry for array_reverse() might have...

Mac
EvanClark
Forum Newbie
Posts: 9
Joined: Thu Aug 22, 2002 1:36 am
Contact:

Post by EvanClark »

Code: Select all

$file_array = file("test.txt"); 
for ( $i = count($file_array) - 1; $i >= 0; $i-- ) &#123; 
echo $file_array&#1111;$i]; 
&#125;
This would also do it - Engineer style.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

this would do it, my style

Code: Select all

$file = array_reverse(file("file.txt"));
foreach($file as $value)&#123; echo $value."<br/>\n"; &#125;
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

hob_goblin you like using foreach don't you.
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

thanks everyone! im going to use the one from EvanClark. its the one that ive seen around alot, and is easiest for me to understand.

and twigletmac, i really dont like the php.net docs, they almost never help me. its good as a refrence sometimes.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

I am used to handling more associative arrays than numerical, it's also a simple concept... and for()'s aren't any faster, if not slower..
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sevengraff wrote:twigletmac, i really dont like the php.net docs, they almost never help me. its good as a refrence sometimes.
Well, you do have to actually look at it to know if it's going to be helpful or not...

As for for vs. foreach - foreach is designed to do the job and is a lot simpler than the for conditional to write out:

Code: Select all

foreach ($file as $value) &#123;
vs.

Code: Select all

for ($i = count($file_array) - 1; $i >= 0; $i--) &#123;
Takuma -> you sure like upping your post count don't you :lol:

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

twigletmac wrote:Takuma -> you sure like upping your post count don't you :lol:
yeah, man, it's not a race...
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

hob_goblin wrote: yeah, man, it's not a race...
Yes it is :lol:
Last edited by mikeq on Wed Aug 28, 2002 3:10 am, edited 2 times in total.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Takuma joined on 4 August and I joined on 3 May and he has nearly as many posts as me.

I must type more, must type more, must type more... :lol:
Post Reply