Jump to content

[SOLVED] in_array() problem :S


Jahren

Recommended Posts

I have this little glimpse of code that wont work

 

function valider_journees($nb_dispos)
{
	$valide = false;
	$date = array();

	//I have to start at 1. its been tested already
	for($i = 1; $i <= $nb_dispos; ++$i)
	{
		$valide = $valide && !in_array($_POST['date' . $i], $date);
		$date .= $_POST['date' . $i];
	}

	return $valide;
}

 

when I echo the values :

echo $_POST['date' . $i] . ' ' . !in_array($_POST['date' . $i], $date);

 

I get this error :

27/01/2009 1

Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Program Files\utilities\EasyPHP 2.0b1\www\gesti\ajouter_benevole.php on line 413

28/01/2009

 

I've tested types with gettype() and they all give me strings

A little help ? Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/135428-solved-in_array-problem-s/
Share on other sites

Try this:

 

<?php
function valider_journees($nb_dispos)
{
$valide = false;
$date = array();

//I have to start at 1. its been tested already
for($i = 1; $i <= $nb_dispos; ++$i)
{
	$valide = $valide && !in_array($_POST['date' . $i], $date);
	$date[] = $_POST['date' . $i];
}

return $valide;
}
?>

      for($i = 1; $i <= $nb_dispos; ++$i)
      {
         $valide = $valide && !in_array($_POST['date' . $i], $date);
         $date .= $_POST['date' . $i];
      }

 

You are setting $date to a string when it should be an array. Thus the error.

 

      $strDate = "";
      for($i = 1; $i <= $nb_dispos; ++$i)
      {
         $valide = $valide && !in_array($_POST['date' . $i], $date);
         $strDate .= $_POST['date' . $i];
      }

 

Try that.

 

This assumes you want it in a string, if you want it in an array, use flyhoney's version.

Well for starters your for is weird, it starts at 1 and adds 1 to it before anything happens, thus you start at 2.

 

   //I have to start at 1. its been tested already
   for($i = 1; $i <= $nb_dispos; $i++)
   {
      $valide = $valide && !in_array($_POST['date' . $i], $date);
      $date[] = $_POST['date' . $i];
   }

 

That would be better, if you do not have a date0 and only have a date1...etc.

 

Also this part:

$valide = $valide && !in_array($_POST['date' . $i], $date);

 

Is weird as $valide will only amount to the last date. So if the last date is in the array, than it will always return false. And if $valide, is false than it will always return false, cause $valide can never be set to true. That statement in itself is flawed.

 

Hope that helps to get you on the right track.

 

I would be more than happy to help more, I just need to know what your goal is for this function. What does it ultimately need to do?

indeed. It was flawed.

I changed the initial value from false to

$valide = $nb_dispos > 0;

 

so if I have no element to check, it returns false.

 

and there is no date0, this is why I start at 1. :)

now it works like a charm

 

Edit : This function is used to verify that there are no double inputs in all the form. I do not know how many entries I have to check before it is sent which is why I have to use that weird setup. (PHP generating Javascript Generating HTML)

Archived

This topic is now archived and is closed to further replies.

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