Jump to content

[SOLVED] Stepping through an Array


waiwai933

Recommended Posts

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;
    }

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.