wannabe_god Posted June 5, 2006 Share Posted June 5, 2006 how can I check if array[$i] is empty?To use like thisCodeif array[$i] != empty //how to check that it is empty{$i++; //lets do next...echo "array position " . $i . " is filled with " . array[$i] . "<br>";}EDITanother question that will solve the problem:how to find the length of an array? Link to comment Share on other sites More sharing options...
aleksanteri Posted June 5, 2006 Share Posted June 5, 2006 Here is the valid code in syntax... but I didn't know you can have empty arrays. (no wonder, I just got my PWS support PHP) for($i = 0; $i <= SomeFunctionThatTellsTheCellAmount; ++$i){if($arr[$i] != "") // check that it is empty{$i++; // lets do next...echo "array position " . $i . "is filled with " . array[$i] . " <br />";}} I dunno does this work but have a try. Link to comment Share on other sites More sharing options...
wannabe_god Posted June 5, 2006 Author Share Posted June 5, 2006 I don't really see what you added apart of a for loop who doesn't help me mutch becouse I did not have a function to tell the cellamount.However, I just found out, (well, I asked someone...) that count($input) counts the fields of the array. Thanks for trying to help, though.this works... for($i=0;$i<count($input);$i++) echo $i+1 . ": " . $input[$i] . "<br>"; Link to comment Share on other sites More sharing options...
justsomeguy Posted June 5, 2006 Share Posted June 5, 2006 The count function returns the length of an array. You can use these functions for determining various things about a variable:if (empty($array))if (isset($array))if (is_array($array))if (is_integer($array[$i]))if (gettype($array) == "array")http://www.php.net/manual/en/ref.var.php Link to comment Share on other sites More sharing options...
aleksanteri Posted June 6, 2006 Share Posted June 6, 2006 Then it should be like: foreach($array as $val){if (empty($array)) // check that it is not empty{echo "array position " . $i . "is filled with " . array[$i] . " <br />";}} Link to comment Share on other sites More sharing options...
aspnetguy Posted June 6, 2006 Share Posted June 6, 2006 Then it should be like:foreach($array as $val){if (empty($array)) // check that it is not empty{echo "array position " . $i . "is filled with " . array[$i] . " <br />";}} <{POST_SNAPBACK}> If the $array is empty nothing inside the forech loop will get executed. Link to comment Share on other sites More sharing options...
wannabe_god Posted June 6, 2006 Author Share Posted June 6, 2006 in every case, it works :)thx, if empty($array[$i]) is what i needed :)ps @Aleksanteri: with only one action, you don't need { and } Link to comment Share on other sites More sharing options...
justsomeguy Posted June 6, 2006 Share Posted June 6, 2006 Then it should be like:foreach($array as $val){if (empty($array)) // check that it is not empty{echo "array position " . $i . "is filled with " . array[$i] . " <br />";}} $i is also undefined. You were probably thinking of this:foreach($array as $key => $val){ echo "array position " . $key . "is filled with " . $val . " <br />";} In that case you don't even need to check if it is empty, because you are iterating through each element. The foreach loop will not execute if the array is empty. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now