Array issue

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

Post Reply
tr3online
Forum Newbie
Posts: 16
Joined: Sat Jan 24, 2009 8:02 pm

Array issue

Post by tr3online »

Hi there,

Say I wanted to save every value of an array on a separate line starting on entry four, for multiple sources of data. The length of the array varies depending on where the data is grabbed from, so it can range anywhere from 10-100. Is there any way to do this?

Example ($matches is the array):

Code: Select all

 
for ( $array_no=4; $array_no<=[color=#40BFFF]max array variable here?[/color]; $array_no=$array_no+1 )
 
{
 
 $line = $matches[1][$array_no];
 fwrite($file, $line."\n");
 
}
 
Thanks!
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Array issue

Post by php_east »

Code: Select all

$line = '';
for ( $array_no=4; $array_no<=[color=#40BFFF]max array variable here?[/color]; $array_no=$array_no+1 )
{
 $line .= strval($matches[1][$array_no])."\n";
 }
fwrite($file, $line."\n");
tr3online
Forum Newbie
Posts: 16
Joined: Sat Jan 24, 2009 8:02 pm

Re: Array issue

Post by tr3online »

That doesn't address the last line of the array issue I'm having :\

I basically want to write a file(txt) from $matches[1][4] -> $matches[1][last array value] with a \n in between each $matches

(If the array 31 large, itll go from $matches[1][4]\n -> $matches[1][31]\n)
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Array issue

Post by php_east »

what are you saying, that you do not know what the array size is ?
the corrected code loops through all your array items, starting from 4. maybe you want to rephrase your question.
tr3online wrote:(If the array 31 large, itll go from $matches[1][4]\n -> $matches[1][31]\n)
yes, that what it does. what is the problem i don't understand.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Array issue

Post by php_east »

count($matches[1]) ? is that what you are looking for ?
tr3online
Forum Newbie
Posts: 16
Joined: Sat Jan 24, 2009 8:02 pm

Re: Array issue

Post by tr3online »

count(matches[1]); is exactly what I wanted :)

Thanks a bunch!
Post Reply