Jump to content

[SOLVED] Looping arrays


foreverhex

Recommended Posts

I don't know if this is possible. It sounds like it should be. Hopefully I can explain, but here I go:

I have an array that is combined with varibles that say either 1 or 0 (true or false). What I want to do if possible, is take that array, everytime the arrays value = 1 ($array[0] = 1) show a script. Which will be a while loop also to handle some mysql arrays.  My main goal is so I don't have to copy paste the while script over and over. Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/83362-solved-looping-arrays/
Share on other sites

To me it sounds like your having a problem cycling through your array.  Is that your main issue?  If so try something like:

 

$arrayCount = count($array);
$X = 0;
while($X <= $arrayCount){
     
     if($array[$X] == 1){
          //Conduct  your test for 1 or 0 here and run your while loop here if satisfied.
     }
     $countX++;

}

Link to comment
https://forums.phpfreaks.com/topic/83362-solved-looping-arrays/#findComment-424112
Share on other sites

There are a number of ways in PHP to loop through arrays "for","foreach","while" come to mind.

 

"for" example:

<?php
for ($i = 0;$i < count($array);$i++) {
//
//  do the work
//
}
?>

 

"foreach" example:

<?php
foreach ($array as $ind => $val) {
//
//  do the work
//
}
?>

 

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/83362-solved-looping-arrays/#findComment-424132
Share on other sites

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.