Dysan Posted November 22, 2007 Share Posted November 22, 2007 Hi, Why does the following error message get displayed, upon entering the first value/ID into the array? How do I prevent this error from happening? Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Documents and Settings\User\Desktop\Xampp\htdocs\1.php on line 7. <?php session_start(); $array = array(); $array = $_SESSION["somename"]; if (!in_array($_GET['id'], $array)) { $array[] = $_GET['id']; } else { echo "ID Exists"; } $_SESSION["somename"] = $array; print_r($array); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted November 22, 2007 Share Posted November 22, 2007 What is in $_SESSION["somename"]? Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 The Array $array = $_SESSION["somename"]; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 22, 2007 Share Posted November 22, 2007 Does $_SESSION["somename"] actually contain an array? If it is PHP should not be outputting that error. In the following bit of code: $array = array(); $array = $_SESSION["somename"]; You setup a variable called $array as an array data type, however on the next line your assign it the value of the $_SESSION["somename"] variable. Now if that variable does not contain an array PHP will change $array's datatype to a string. WHich will result in the in_array function returning the above error message Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 So what needs to be changed? Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 you cant send arrays directly check serialize() unsirialize() functions www.php.net/serialize Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 22, 2007 Share Posted November 22, 2007 Hold on... I didn't read your script fully and now I understand what you are trying to do. You'll want to change the following two lines: $array = array(); $array = $_SESSION["somename"]; to: $array = ( isset($_SESSION['somename']) && is_array($_SESSION['somename']) ) ? $_SESSION['somename'] : array(); EDIT: @snk you cant send arrays directly check serialize() unsirialize() functions www.php.net/serialize PHP automaticaly serializes session data. No need to serialize/unserialize arrays being stored within a session. Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 Perfect! May I ask what you did? How do I carryout what you did using a long if statement, e.g. if......else....? Quote Link to comment Share on other sites More sharing options...
Cagecrawler Posted November 22, 2007 Share Posted November 22, 2007 He's used a ternary operator to choose whether to use the SESSION array or create a new one. Using If else: <?php if(isset($_SESSION['somename']) && is_array($_SESSION['somename'])) { $array = $_SESSION['somename']; } else { $array = array(); } ?> Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 wildteen88 is it right to say.. in file a.php <?php //lets say session has been started $Stringtest = "one-two-three"; $st2 = explode("-",$Stringtest); $_SESSION['st'] = $st2; ?> in file b.php <?php $st = $_SESSION['st']; echo $st[0]; ?> will the result of echo be one ? thanks and sorry if im sledding from the subject Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 22, 2007 Share Posted November 22, 2007 The following line is an in-line if/else statement (most commonly called a ternary operator): $array = ( isset($_SESSION['somename']) && is_array($_SESSION['somename']) ) ? $_SESSION['somename'] : array(); Converted into a normal if/else statement: if( isset($_SESSION['somename']) && is_array($_SESSION['somename']) ) { $array = $_SESSION['somename']; } else { $array = array(); } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 22, 2007 Share Posted November 22, 2007 wildteen88 is it right to say.. in file a.php <?php //lets say session has been started $Stringtest = "one-two-three"; $st2 = explode("-",$Stringtest); $_SESSION['st'] = $st2; ?> in file b.php <?php $st = $_SESSION['st']; echo $st[0]; ?> will the result of echo be one ? thanks and sorry if im sledding from the subject Correct Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 How do I remove a key from the array? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 22, 2007 Share Posted November 22, 2007 unset($array['key']); //unset the element "key" Orio. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 22, 2007 Share Posted November 22, 2007 Use unset eg: unset($array['key_name_here']) EDITE Orio beat me Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 How do I search the array for a specific value, then delete that value from the array? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 22, 2007 Share Posted November 22, 2007 Use array_keys() with the optional search parameter. Then go over the results and delete them: <?php foreach(array_keys($array, "search value") as $k=>$v) unset($array[$k]); ?> Orio. Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 How do I retrieve the id from a link (example below), search the array for that id number, then delete that id number in the array? Link Example: www.site.com/data.php?id=22 Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 you set the site into a string \nd with explode u take it $v = www.site.com/data.php?id=22 ; $d = explode("=",$v); echo $d[1]; // or $e = d[1]; Quote Link to comment Share on other sites More sharing options...
Orio Posted November 22, 2007 Share Posted November 22, 2007 A bit of regex... <?php $str = "www.site.com/data.php?id=22"; preg_match("/data\.php\?.*?id=(\d+)/", $str, $matches); echo $matches[1]; ?> Orio. Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 mine is easier thanks for the post i need to use your code, i have a similar issue. Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 How do I create a displayShoppingCart() function, that displays the total amount of items inside the array, or displays the message "Shopping Cart is Empty", if the array is empty? How do I create a switch statement, based on a action variable, then add the code to add data to the array to a case statement called "add" inside the switch statement? Each time an item/id is added to the array, how do I display the shopping cart. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 22, 2007 Share Posted November 22, 2007 mine is easier thanks for the post i need to use your code, i have a similar issue. You are right, yours is simpler. But yours is very very very specific and will work only if the url ends with "id=<num>" Mine will work even if there's more url parameters, not only the id, and id doesn't have to be in the end to make it work- it can be in the middle: "domain.com/data.php?bla=1&id=2&bla2=44" Will match. Orio. Quote Link to comment Share on other sites More sharing options...
snk Posted November 22, 2007 Share Posted November 22, 2007 yep i know.. thus i thanked you.. i had the same issue and i was putting my variable at the end. when i wanted to take second variable in the middle i was doing explode 2 times more. thanks again orio. Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 22, 2007 Author Share Posted November 22, 2007 Why doesn't the amount of values in the array get displayed, to reflect what in the array, upon calling the writeShoppingCart() function? - The amount stays at 0, Why is this? <?php session_start(); function writeShoppingCart() { echo count($array); } if(isset($_SESSION['ids']) && is_array($_SESSION['ids'])) { $array = $_SESSION['ids']; } else { $array = array(); } if (!in_array($_GET['id'], $array)) { $array[] = $_GET['id']; } else { echo "ID Exists"; } $_SESSION['ids'] = $array; writeShoppingCart() print_r($array); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.