Jump to content

array session and in_array help


MDanz

Recommended Posts

The result should echo 1.  $_SESSION[50] is an array.

i keep getting this error... Warning: in_array() [function.in-array]: Wrong datatype for second argument in..

 

$value="50";
$search = "test";
if(in_array($search,$_SESSION[$value])) {

	$duplicate = 1;

	}else {
$duplicate = 0;

}
echo $duplicate;

Link to comment
https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/
Share on other sites

in_array([search text],[array haystack]);

 

I don't really get what you need (especially the $_SESSION) but probably

 

$value=array("50","100");

$search="test";

if(in_array($search,$value)) {

 

$duplicate = 1;

 

}else {

$duplicate = 0;

 

}

echo $duplicate;

$_SESSION['50']=$value

??????

 

I dont really get that part...do u mean this?

 

$value=array("50","100");

$_SESSION['myArray']=$value;

$search="test";

if(in_array($search,$_SESSION['myArray'])) {

     

      $duplicate = 1;

     

      }else {

$duplicate = 0;

 

}

echo $duplicate;

If $_SESSION[50] actually contains an array, your code would do what you expect.

 

What's your actual code that reproduces the problem?

 

What does the following show for the $_SESSION array -

echo '<pre>',print_r($_SESSION,true),'</pre>';

i have an array stored in the session.

 

e.g.

$value=array("50","100");

$_SESSION[50] = $value;

 

ok how would i revert the session back to the array form. so in_array works.

 

here is a different strategy

 

$search="test";
$value=array("50","100");
$_SESSION[50] = $value;
foreach($_SESSION[50] as $key=>$val){

   if($val == $search) {print "match found";}
}

yup..if this your full code then the error is because the $_SESSION doesnt contain any array. you only create a $_SESSION['50'] but nothing inside the session. see the bold one

 

$value="50";

$search = "test";

if(in_array($search,$_SESSION[$value])) {

 

$duplicate = 1;

 

}else {

$duplicate = 0;

 

}

echo $duplicate;

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.