waiwai933 Posted January 26, 2009 Share Posted January 26, 2009 Hi there. I have an array that I want to step through, and check if anything is equal to 1. If it is, then another variable is set to 0, otherwise if everything is 0, the variable is set to 1. Would the following code work? <?php $testnumber=0; while ($testnumber<9) { if($fail[$testnumber]==0) { $anothernumber++; } $testnumber++; } if($anothernumber>0) { $thirdnumber=0; } else { $thirdnumber=1; } Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/ Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 Why not just use array_search to locate if a value is one? Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746162 Share on other sites More sharing options...
waiwai933 Posted January 26, 2009 Author Share Posted January 26, 2009 Well, I don't want to know where a value is zero. I just want to know if a value is zero. Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746166 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 <?php $testArray = array(1, 2, 3, 0); $otherVal = 1; foreach ($testArray as $key =>$value) { if ($value == 1) { $otherVal = 0; } } if ($otherVal != 0) { $anotherVal = 1; } ?> foreach will make it easier for ya. Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746170 Share on other sites More sharing options...
waiwai933 Posted January 26, 2009 Author Share Posted January 26, 2009 I'm sorry, I'm completely confused over that code. Could you add a couple of comments in there just so that I can learn what you're doing? Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746173 Share on other sites More sharing options...
.josh Posted January 26, 2009 Share Posted January 26, 2009 Hi there. I have an array that I want to step through, and check if anything is equal to 1. If it is, then another variable is set to 0, otherwise if everything is 0, the variable is set to 1. Would the following code work? So are all of the values of this array going to be 1, or else they are all going to be 0, as in, it's always going to be one of these? $array = array(1,1,1,1,1); or $array = array(0,0,0,0,0); ? If so.... $somevar = (array_sum($array) == count($array))? 0 : 1; Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746176 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 I'm sorry, I'm completely confused over that code. Could you add a couple of comments in there just so that I can learn what you're doing? Really it is straight forward. See foreach it will iterate through the array and assign those values to $value. Basically it does a loop, but there is no guess, like with the while loop of how many array entries their are. If the value of this index of the array is 1 then you set the $otherval equal to 0. After all the elements have been looped through you check if $otherVal is not equal to 0, if it is not, that means that no element in the array equaled 1, so you set anotherVal equal to one... Should be exactly what you described in your post description. If that is now what you want, please explain exactly what is you want to do. I would also suggest reading up on array's. Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746177 Share on other sites More sharing options...
waiwai933 Posted January 26, 2009 Author Share Posted January 26, 2009 Ok, that helps. Just two questions: 1. What does as $key =>$value do? I looked it up in the manual but couldn't make heads or tails of it. 2. When I use the code, it also outputs the following warning: Invalid argument supplied for foreach() in /directory/example.php on line 109 . How should I fix this? (Line 109 is the line that has the foreach) Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746216 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 All arrays have keys, or indexes as they say. A regular array is 0-based. So $array[0] will print out the first element of the array. There are also associative arrays, meaning the keys are actual words. So $array['first'] would print out the value located at index 'first' So for each element in the array, assign the index of that array to $key and the value at that index to $value. The invalid argument means you are not using an array. Make sure the value you are using is an array with is_array before using it to avoid that warning in the future. Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746221 Share on other sites More sharing options...
waiwai933 Posted January 26, 2009 Author Share Posted January 26, 2009 Ah, thank you. That explains why it only outputs an error when all the terms are 0. Is there a way to hide that warning since it's what I want to happen? Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746228 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 Ah, thank you. That explains why it only outputs an error when all the terms are 0. Is there a way to hide that warning since it's what I want to happen? Use the if statement with is_array, if it is not an array then do not run the foreach. Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746230 Share on other sites More sharing options...
waiwai933 Posted January 26, 2009 Author Share Posted January 26, 2009 Alright, thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/142414-solved-stepping-through-an-array/#findComment-746233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.