Page 1 of 2
echo a file contents backwards.
Posted: Mon Aug 26, 2002 11:52 pm
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++ ) {
echo $file_arrayї$i];
}
but i need it to do it in reverse order (so the last line is displayed first). what do i do?
Posted: Tue Aug 27, 2002 12:08 am
by JPlush76
http://www.php.net/manual/en/function.array-reverse.php
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.
Posted: Tue Aug 27, 2002 12:34 am
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.
Posted: Tue Aug 27, 2002 12:53 am
by protokol
Code: Select all
$file_array = file("test.txt");
$contents = "";
for ($i = 0; $i < count($file_array); $i++)
$contents .= $file_arrayї$i];
echo strrev($contents);
Posted: Tue Aug 27, 2002 12:58 am
by Sevengraff
Thanks!!!
Posted: Tue Aug 27, 2002 2:57 am
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
Posted: Tue Aug 27, 2002 3:06 am
by EvanClark
Code: Select all
$file_array = file("test.txt");
for ( $i = count($file_array) - 1; $i >= 0; $i-- ) {
echo $file_arrayї$i];
}
This would also do it - Engineer style.
Posted: Tue Aug 27, 2002 11:42 am
by hob_goblin
this would do it, my style
Code: Select all
$file = array_reverse(file("file.txt"));
foreach($file as $value){ echo $value."<br/>\n"; }
Posted: Tue Aug 27, 2002 3:20 pm
by Takuma
hob_goblin you like using foreach don't you.
Posted: Tue Aug 27, 2002 7:14 pm
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.
Posted: Tue Aug 27, 2002 11:39 pm
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..
Posted: Wed Aug 28, 2002 2:05 am
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:
vs.
Code: Select all
for ($i = count($file_array) - 1; $i >= 0; $i--) {
Takuma -> you sure like upping your post count don't you
Mac
Posted: Wed Aug 28, 2002 2:13 am
by hob_goblin
twigletmac wrote:Takuma -> you sure like upping your post count don't you
yeah, man, it's not a race...
Posted: Wed Aug 28, 2002 3:09 am
by mikeq
hob_goblin wrote:
yeah, man, it's not a race...
Yes it is

Posted: Wed Aug 28, 2002 3:12 am
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...
