Jump to content

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;

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.