MDanz Posted July 7, 2011 Share Posted July 7, 2011 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 More sharing options...
robert_gsfame Posted July 7, 2011 Share Posted July 7, 2011 sure you got that error cause $_SESSION[$value] is not an array Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239700 Share on other sites More sharing options...
robert_gsfame Posted July 7, 2011 Share Posted July 7, 2011 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; Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239702 Share on other sites More sharing options...
MDanz Posted July 7, 2011 Author Share Posted July 7, 2011 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. Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239703 Share on other sites More sharing options...
robert_gsfame Posted July 7, 2011 Share Posted July 7, 2011 $_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; Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239708 Share on other sites More sharing options...
MDanz Posted July 7, 2011 Author Share Posted July 7, 2011 yes thats what i mean. when i tried that i got the Warning: in_array() [function.in-array]: Wrong datatype for second argument in.. Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239710 Share on other sites More sharing options...
PFMaBiSmAd Posted July 7, 2011 Share Posted July 7, 2011 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>'; Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239711 Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 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";} } Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239713 Share on other sites More sharing options...
robert_gsfame Posted July 7, 2011 Share Posted July 7, 2011 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; Link to comment https://forums.phpfreaks.com/topic/241344-array-session-and-in_array-help/#findComment-1239715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.