0

I am trying to update a serialized array that was generated using the php below...

// my php and array serialization
$myArray = array( 161 => true, 180 => true );
$myData = serialize($myArray);
setcookie('my_cookie', $myData);

I am using jQuery to get this serialised data from my cookie.

// remove item click function
$('.remove-item').click( function(e) {

    // get the serialized data
    console.log(Cookies.get('my_cookie'));

});

This is my returned serialzed data

a:2:{i:161;b:1;i:180;b:1;}

I want to be able unserialze this data and remove a item from the array, then serialze the data again and re-set my cookie.

So I would get the value from a data attribute on my .remove-item button, and remove this value from the unserialzed array.

It need it work something like this, I just cant figure out how to actually do it. See below my walk through, but this obviously won't actually work.

jQuery

// remove item click function
$('.remove-item').click( function(e) {

    // get the item id number
    var removeItem = $(this).data('id');

    // 161

    // get the serialized data
    var myData = Cookies.get('my_cookie'));

    // a:2:{i:161;b:1;i:180;b:1;}

    // unserialize data back to an array
    var myArray = unserialize(myData);

    // array(2) { [161]=> bool(true) [180]=> bool(true) }

    // remove 161 from my array
    myArray = $.grep(myArray, function(value) {
        return value != removeItem;
    });

    // array(1) { [180]=> bool(true) }

    // re serialize my array 
    var newData = serialize(myArray);

    // a:1:{i:180;b:1;}

    // re set cookie with new serialized data
    Cookies.set('my_cookie', newData);

});

HTML

<button class="remove-item" data-id="161">Remove Item</button>

Any ideas or pointers would be much appreciated.

Thanks

1 Answer 1

1

For the sake of handling data between the client/server side, rather than using serializing I'd change it to JSON. That way you can just run JSON.parse(data) to return your array of data.

PHP

$myArray = array( 161 => true, 180 => true );
$myData = json_encode($myArray);
setcookie('my_cookie', $myData);

jQuery -

// remove item click function
$('.remove-item').click( function(e) {

    // get the item id number
    var removeItem = $(this).data('id');

    // get the serialized data
    var myData = Cookies.get('my_cookie'));

    // convert cookie string to data
    var myArray = JSON.parse(myData);

    // myArray = {"161":true,"180":true}

    // remove our value
    delete myArray[removeItem];

    // turn the array back to string
    var newData = JSON.stringify(myArray);

    // re set cookie with new serialized data
    Cookies.set('my_cookie', newData);

});

That should sort you out :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.