ecopetition Posted November 21, 2008 Share Posted November 21, 2008 Hello, I've got the following array output (when printy_r'ed) Array ( [0] => Array ( [0] => O1 [1] => O2 [2] => O3 [3] => O4 [4] => O5 [5] => O6 [6] => [7] => [8] => [9] => ) [1] => Array ( [0] => O1 [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) [2] => Array ( [0] => O1 [1] => O2 [2] => O3 [3] => O4 [4] => O5 [5] => O6 [6] => O7 [7] => O8 [8] => O9 [9] => O10 ) ) How can I get the PHP to tell me how many of the fields are not blank in each set? Like for [0], the number of fields with content is 6. How can I make the PHP return this number for each set in the array. (something like [0] => 6, [1] => 1, [2] => 10). I can only guess that this will require a "for" command. Thanks a lot for your help. Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/ Share on other sites More sharing options...
ecopetition Posted November 21, 2008 Author Share Posted November 21, 2008 I tried count() on this but it's coming out as 10 for each set even though 4 of the sets in the first one, for example, are blank. Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-695717 Share on other sites More sharing options...
iversonm Posted November 21, 2008 Share Posted November 21, 2008 i would suggest looping through and counting each of them saying them as a session something like this foreach($array=$blah){ if(!$blah|| strlen($blah= trim($blah)) == 0){ if(isset($_SESSION['empty_array'])){ $_SESSION['empty_array']++; }else{ $_SESSION['empty_array']=1; } } } then your session will tell you how many are blank? you could also save which array its in to know specifically which ones are blank does that help at all? Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-695723 Share on other sites More sharing options...
ecopetition Posted November 21, 2008 Author Share Posted November 21, 2008 Thanks but I don't want to start using sessions for something like this, I'm sure there's a simpler way available (though I'm no pro so it could be the only way ). Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-695727 Share on other sites More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 There's no need to use sessions at all for simply counting $count = 0; foreach($array as $value){ if (trim($value) > '') { $count++; } } Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-695774 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 Thanks but how do I get an output for each set in the array? So, for set [0], get an output of 6, etc. Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696167 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2008 Share Posted November 22, 2008 This is a job for array_filter <?php $test = Array (Array('O1','02','03','04','05','06','','','',''), Array('01','','','','','','','','',''), Array('O1','02','03','04','05','06','07','08','09','010')); $i = 0; foreach ($test as $ta) echo 'Array ' . $i++ . ' has ' . count(array_filter($ta)) . ' non-blank entries<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696172 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 Thanks a lot Ken. I really don't want to sound lazy but I just don't have a clue how to do apply that to my problem, even after reading the manual. Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696173 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2008 Share Posted November 22, 2008 My sample code does exactly what you've said you wanted to do. Is your problem something other than what you stated? Ken Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696175 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 Yes sorry, I forgot to mention that the array at the start varies. It's values aren't always what they are in the first post (it's a user-controlled form). Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696177 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2008 Share Posted November 22, 2008 Then please post a more realistic example and describe what you want to do. Ken Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696180 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 Ok, this array is all based around a poll form: <input type="text" name="poll_title" style="width:300px;margin-top:1px;margin-bottom:1px;" value="" /><br/> Question 1: <input type="text" name="poll_questions[]"/><br/> Option 1: <input type="text" name="poll_options[]"/><br/> Option 2: <input type="text" name="poll_options[]"/><br/> Option 3: <input type="text" name="poll_options[]"/><br/> Option 4: <input type="text" name="poll_options[]"/><br/> Option 5: <input type="text" name="poll_options[]"/><br/> Option 6: <input type="text" name="poll_options[]"/><br/> Option 7: <input type="text" name="poll_options[]"/><br/> Option 8: <input type="text" name="poll_options[]"/><br/> Option 9: <input type="text" name="poll_options[]"/><br/> Option 10: <input type="text" name="poll_options[]"/><br/> <br/> Question 2: <input type="text" name="poll_questions[]"/><br/> Option 1: <input type="text" name="poll_options[]"/><br/> Option 2: <input type="text" name="poll_options[]"/><br/> Option 3: <input type="text" name="poll_options[]"/><br/> Option 4: <input type="text" name="poll_options[]"/><br/> Option 5: <input type="text" name="poll_options[]"/><br/> Option 6: <input type="text" name="poll_options[]"/><br/> Option 7: <input type="text" name="poll_options[]"/><br/> Option 8: <input type="text" name="poll_options[]"/><br/> Option 9: <input type="text" name="poll_options[]"/><br/> Option 10: <input type="text" name="poll_options[]"/><br/> <br/> Question 3: <input type="text" name="poll_questions[]"/><br/> Option 1: <input type="text" name="poll_options[]"/><br/> Option 2: <input type="text" name="poll_options[]"/><br/> Option 3: <input type="text" name="poll_options[]"/><br/> Option 4: <input type="text" name="poll_options[]"/><br/> Option 5: <input type="text" name="poll_options[]"/><br/> Option 6: <input type="text" name="poll_options[]"/><br/> Option 7: <input type="text" name="poll_options[]"/><br/> Option 8: <input type="text" name="poll_options[]"/><br/> Option 9: <input type="text" name="poll_options[]"/><br/> Option 10: <input type="text" name="poll_options[]"/><br/> <input type="submit" name="poll_submit" value="Create Poll" /> On submit, it's to run the following script: $poll_title = (isset($_POST['poll_title'])) ? htmlspecialchars($_POST['poll_title']) : ''; $poll_options = array(); $num_poll_options = 0; $poll_options_post = (isset($_POST['poll_options'])) ? $_POST['poll_options'] : array(); $poll_questions = array(); $num_poll_questions = 0; $poll_questions_post = (isset($_POST['poll_questions'])) ? $_POST['poll_questions'] : array(); for($i = 0; $i < count($poll_questions_post); $i++) { $poll_questions[] = htmlspecialchars($poll_questions_post[$i]); if(! empty($poll_questions[$i])) { $num_poll_questions++; } } for($i = 0; $i < count($poll_options_post); $i++) { $poll_options[] = htmlspecialchars($poll_options_post[$i]); if(! empty($poll_options_post[$i])) { $num_poll_options++; } } if(empty($poll_title)) { trigger_error('no_title_specified'); } for($i = 0; $i < count($poll_questions); $i++) { if(empty($poll_questions[$i])) { continue; } $sql = "INSERT INTO poll_questions (question_text) VALUES('". $db->escape($poll_questions[$i]) ."')"; $db->query($sql); } for($i = 0; $i < count($poll_questions); $i++) { if(empty($poll_questions[$i])) { continue; } $poll_id = $db->last_id(); foreach($poll_options as $k => $v) { if($v != '') { $sql = "INSERT INTO poll_options (question_id, option_text) VALUES('". $poll_questions[$i] ."', '". $v ."')"; $db->query($sql); } } break; } $sql = "INSERT INTO polls (poll_title, topic_id) VALUES('$poll_title', $topic_id)"; $db->query($sql); $sql = "UPDATE topics SET has_poll = 1 WHERE topic_id = $topic_id"; $db->query($sql); define('META_REFRESH', "viewtopic.php?t=$topic_id&f=$forum_id"); trigger_error('poll_added'); This form is modifiable, so if the website admin wants users to post 5 questions each with 20 options, it's allowed through editing the database. All questions, however, need to have at least 2 options for the user to select (when the "poll_questions[]" field is specified). So, I need to get a count of each set in the array and return an error when the count is <2 AND poll_questions[] is specified. That basically sums it up. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696183 Share on other sites More sharing options...
.josh Posted November 22, 2008 Share Posted November 22, 2008 @ken: argh! I was looking around for a 1-line solution and almost used array_filter but skipped it because I didn't read far enough into the manual to see the default w/out 2nd argument! I came up with this for the foreach loop: count(array_unique($a)) - 1; Yes sorry, I forgot to mention that the array at the start varies. It's values aren't always what they are in the first post (it's a user-controlled form). It's either got a value, or it doesn't, and you want to count the ones with values, right? I don't see why different values should make a difference. Ken's code inside the foreach loop is merely an example. It displays the magic numbers your looking for. Instead of echoing it, you would throw a condition in there. Something like: foreach ($test as $ta) { if (count(array_filter($ta)) < 2) { // we've got problems. less than 2 were selected } } Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696185 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 Crayon - when I try your code with my parameters I get the following error: [Warning] array_filter() [function.array-filter]: The first argument should be an array (Line 718, File post.php) That is, foreach ($poll_options as $ta) { if (count(array_filter($ta)) < 2) { trigger_error('less than 2 specifications'); } Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696289 Share on other sites More sharing options...
corbin Posted November 22, 2008 Share Posted November 22, 2008 You're passing it the wrong thing.... I'm guessing ta is a string. Do it like: if(count(array_filter($poll_options)) < 2) { } Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696305 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 But Corbin, that's still referring to sets [0], [1], and [2], and not each one specifically. Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696316 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 Does anyone have the correct expertise to help? Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696410 Share on other sites More sharing options...
.josh Posted November 22, 2008 Share Posted November 22, 2008 I think your lack of response is due to people getting the impression you aren't putting a whole lot of your own effort into it...you have your error. Read the documentation for the function. Better yet, read the error. You are getting that error because the function expects the argument to be an array. I'm guessing that happens when you don't select anything, right? If so, inside the foreach loop, check to see if $ta is an array before executing that if statement. If it's not, then kick back your error. If it is, then run the condition to kick back the same error if it has less than 2 elements. You can even combine that into a single condition. Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696419 Share on other sites More sharing options...
ecopetition Posted November 22, 2008 Author Share Posted November 22, 2008 I really don't want to sound lazy but I just don't have a clue how to do apply that to my problem, even after reading the manual. Quote Link to comment https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696426 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.