Monkuar Posted March 7, 2012 Share Posted March 7, 2012 Here is my code if (isset($_GET['hide'])){ $id = intval($_GET['hide']); $explode = explode(",", $_COOKIE['hide']); if (in_array($id, $explode)) { } setcookie('hide', ''.$id.'',time()+32000000); header('Location: index.php'); exit; } My Explanation: If somone clicks ?hide=2 It will add the cookie hide, and the value to 2. Now, if somone clicks ?hide=1, I need it to add 2,1 how is that possible? Then if the in_array is correct, (Means if the value of the cookie is 2 and they clicked ?hide=2) how do I take that number "2" out of the it (Remove it) and make a new array that will be inserted to my $id variable? Quote Link to comment https://forums.phpfreaks.com/topic/258491-how-to-array-replace/ Share on other sites More sharing options...
marcus Posted March 7, 2012 Share Posted March 7, 2012 $id = 2; $array = array(1,2,3); if(in_array($id, $array)){ $array = array_diff($array,array($id)); } $cookie = setcookie('hide', implode(',', $array), time()+32000000); My approach... $id is my equivalence to your get variable, $array is my equivalence to your $explode variable. If the $id in the $array -> redefine $array as the difference between the current $array and a new array containing the $id. Then change the cookie's value from "1,2,3" to "1,3" Quote Link to comment https://forums.phpfreaks.com/topic/258491-how-to-array-replace/#findComment-1325010 Share on other sites More sharing options...
requinix Posted March 8, 2012 Share Posted March 8, 2012 Stop creating thread after thread after thread for the exact same problem! It's annoying! Quote Link to comment https://forums.phpfreaks.com/topic/258491-how-to-array-replace/#findComment-1325028 Share on other sites More sharing options...
Monkuar Posted March 8, 2012 Author Share Posted March 8, 2012 $id = 2; $array = array(1,2,3); if(in_array($id, $array)){ $array = array_diff($array,array($id)); } $cookie = setcookie('hide', implode(',', $array), time()+32000000); My approach... $id is my equivalence to your get variable, $array is my equivalence to your $explode variable. If the $id in the $array -> redefine $array as the difference between the current $array and a new array containing the $id. Then change the cookie's value from "1,2,3" to "1,3" Hey, this works but what if I want to add to the array instead of take away stuff? Is there a different function for that? I added this else{ setcookie('hide', ''.$id.'', time()+32000000); } to the end, so it can just add the ?hide=1 if somone clicked, so it will actually set my cookie value, but then now if the user clicks on ?hide=2, it will launch this again because that $id is not matching the in_array Quote Link to comment https://forums.phpfreaks.com/topic/258491-how-to-array-replace/#findComment-1325031 Share on other sites More sharing options...
marcus Posted March 8, 2012 Share Posted March 8, 2012 Adding to an array is just using the square bracket syntax. $array = array(1,2,3); $array[] = 4; // array is now [1,2,3,4] Quote Link to comment https://forums.phpfreaks.com/topic/258491-how-to-array-replace/#findComment-1325036 Share on other sites More sharing options...
Monkuar Posted March 8, 2012 Author Share Posted March 8, 2012 Adding to an array is just using the square bracket syntax. $array = array(1,2,3); $array[] = 4; // array is now [1,2,3,4] Okay awesome if (isset($_GET['hide'])){ $id = intval($_GET['hide']); $explode = explode(",", $_COOKIE['hide']); $id = $id; $array = $explode; if(in_array($id, $array)){ $array = array_diff($array,array($id)); setcookie('hide', implode(',', $array), time()+32000000); }else if ($_COOKIE['hide'] AND $explode['1'] AND $explode['2']){ setcookie('hide', ''.$explode['0'].','.$explode['1'].','.$explode['2'].','.$id.'', time()+32000000); }else if ($_COOKIE['hide'] AND $explode['1']){ setcookie('hide', ''.$explode['0'].','.$explode['1'].','.$id.'', time()+32000000); } else if ($_COOKIE['hide']){ setcookie('hide', ''.$explode['0'].','.$id.'', time()+32000000); } else{ setcookie('hide', ''.$id.'', time()+32000000); } header('Location: index.php'); exit; } my new code works awesome bro Thanks for your help. Marking as Resolved!! Quote Link to comment https://forums.phpfreaks.com/topic/258491-how-to-array-replace/#findComment-1325042 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.