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
Sevengraff
Forum Contributor
Posts: 232 Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:
Post
by Sevengraff » Mon Aug 26, 2002 11:52 pm
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?
JPlush76
Forum Regular
Posts: 819 Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:
Post
by JPlush76 » Tue Aug 27, 2002 12:08 am
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.
Sevengraff
Forum Contributor
Posts: 232 Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:
Post
by Sevengraff » Tue Aug 27, 2002 12:34 am
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.
protokol
Forum Contributor
Posts: 353 Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:
Post
by protokol » Tue Aug 27, 2002 12:53 am
Code: Select all
$file_array = file("test.txt");
$contents = "";
for ($i = 0; $i < count($file_array); $i++)
$contents .= $file_arrayї$i];
echo strrev($contents);
Sevengraff
Forum Contributor
Posts: 232 Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:
Post
by Sevengraff » Tue Aug 27, 2002 12:58 am
Thanks!!!
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Aug 27, 2002 2:57 am
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 » Tue Aug 27, 2002 3:06 am
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.
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Tue Aug 27, 2002 11:42 am
this would do it, my style
Code: Select all
$file = array_reverse(file("file.txt"));
foreach($file as $value){ echo $value."<br/>\n"; }
Takuma
Forum Regular
Posts: 931 Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:
Post
by Takuma » Tue Aug 27, 2002 3:20 pm
hob_goblin you like using foreach don't you.
Sevengraff
Forum Contributor
Posts: 232 Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:
Post
by Sevengraff » Tue Aug 27, 2002 7:14 pm
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.
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Tue Aug 27, 2002 11:39 pm
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..
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Wed Aug 28, 2002 2:05 am
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
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Wed Aug 28, 2002 2:13 am
twigletmac wrote: Takuma -> you sure like upping your post count don't you
yeah, man, it's not a race...
mikeq
Forum Regular
Posts: 512 Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland
Post
by mikeq » Wed Aug 28, 2002 3:09 am
hob_goblin wrote:
yeah, man, it's not a race...
Yes it is
Last edited by
mikeq on Wed Aug 28, 2002 3:10 am, edited 2 times in total.
mikeq
Forum Regular
Posts: 512 Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland
Post
by mikeq » Wed Aug 28, 2002 3:12 am
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...