Jump to content

how to array replace?


Monkuar

Recommended Posts

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?

Link to comment
Share on other sites

$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"

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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!!

 

Link to comment
Share on other sites

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.