|
-
Jul 21st, 2005, 01:44 PM
#1
Thread Starter
Admodistrator
Loop thru an array
Is there a way to loop thru an array?
$split = explode(":",$contents)
How do i do somethign which each split[x]?
-
Jul 21st, 2005, 07:48 PM
#2
Re: Loop thru an array
yes of course.
-
Jul 21st, 2005, 07:50 PM
#3
Re: Loop thru an array
let me go check my handy dandy exercises. 
here it is... sorry just placed everything here when I need to test something I read at dox. 
PHP Code:
<?php
/*
spent the last year
rocky mountain way
couldnt get much higher
out to pasture
think its safe to say
time to open fire
*/
$arr = array( 1 => array( 1 => "spent", 2 => "the", 3 => "last", 4 => "year"),
2 => array( 1 => "rocky", 2 => "mountain", 3 => "way"),
3 => array( 1 => "couldnt", 2 => "get", 3 => "much", 4 => "higher"),
4 => array( 1 => "out", 2 => "to", 3 => "pasture"),
5 => array( 1 => "think", 2 => "its", 3 => "safe", 4 => "to", 5=> "say"),
6 => array( 1 => "time", 2 => "to", 3 => "open", 4 => "fire")
);
?>
<html>
<body>
<table>
<?PHP
/*$reccnt = 0;
for($cnt1 = 1; $cnt1 <= count($arr[1]); $cnt1++ ){
for($cnt2 = 1; $cnt2 <= count($arr[2]); $cnt2++){
for($cnt3 = 1; $cnt3 <= count($arr[3]); $cnt3++){
for($cnt4 = 1; $cnt4 <= count($arr[4]); $cnt4++){
for($cnt5 = 1; $cnt5 <= count($arr[5]); $cnt5++){
for($cnt6 = 1; $cnt6 <= count($arr[6]); $cnt6++){
echo "<TR>";
echo "<TD>";
echo ++$reccount;
echo "</TD>";
echo "<TD>";
echo $arr[1][$cnt1];
echo "</TD>";
echo "<TD>";
echo $arr[2][$cnt2];
echo "</TD>";
echo "<TD>";
echo $arr[3][$cnt3];
echo "</TD>";
echo "<TD>";
echo $arr[4][$cnt4];
echo "</TD>";
echo "<TD>";
echo $arr[5][$cnt5];
echo "</TD>";
echo "<TD>";
echo $arr[6][$cnt6];
echo "</TD>";
echo "</TR>";
}
}
}
}
}
} */
?>
</table>
<?PHP
print "<BR>";
print "TESTING";
$detail = array_fill(0, 2, "value");
foreach( $detail as $det) {
print "<BR>".$det;
}
print "/**********************************************************************/"."<BR>";
$mystring = "Iamthestring";
print $mystring."<BR>";
print substr($mystring, 0, 1).substr($mystring, strlen($mystring) - 6).'=Istring';
print '<BR>';
$month = "12";
print date("Y/m/d", mktime(0,0,0, $month - 1, 13, 2005)) . ' - ' . date("Y/m/d", mktime(0,0,0, $month, 13, 2020));
print '<BR>';
$strtoSplit = "1,2,3,4,5,data";
$arraydata = split(",", $strtoSplit );
var_dump( $arraydata);
?>
</body>
</html>
-
Jul 21st, 2005, 07:53 PM
#4
Re: Loop thru an array
check out the foreach part
you could do like
PHP Code:
$aString = "1, 2, 3, 4, 5";
$myArr = split(",", $aString);
foreach($myArr as $x ){
print $x;
}
or just use the for loop like I did above.
Last edited by oceanebelle; Jul 21st, 2005 at 07:55 PM.
Reason: ehehe.. split function was not right. :D
-
Jul 21st, 2005, 07:59 PM
#5
Re: Loop thru an array
oooh.. I learned one function today... explode()... hmm
and the difference.
explode is much faster because no evaluation overhead for regular expressions..
split is used if you'd like to use regular expressions. 
I should read the dox better next time.
-
Jul 22nd, 2005, 12:56 AM
#6
Thread Starter
Admodistrator
Re: Loop thru an array
How do i tell which iteration of the loop i am on?
-
Jul 22nd, 2005, 01:21 AM
#7
Re: Loop thru an array
 Originally Posted by |2eM!x
How do i tell which iteration of the loop i am on?
well you could just use the for loop. 
if you really want to know where you are on... I use the for loop more often .. check out the commented code. i was practising on two dimensional arrays.. populated manually.
-
Jul 22nd, 2005, 01:31 AM
#8
Thread Starter
Admodistrator
Re: Loop thru an array
after i posted i thought about ways to do it
i could simply set a variable and add one x++ or use the for loop
-
Jul 24th, 2005, 07:37 PM
#9
Re: Loop thru an array
yeah like the for loop in my commented sample code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|