Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/133696-array-help-please/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-695723
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696172
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696183
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696185
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696289
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/133696-array-help-please/#findComment-696419
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.