Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
取消序列化数据并从数组中删除项,然后使用jQuery重新序列化数组_Jquery_Arrays_Serialization - Fatal编程技术网

取消序列化数据并从数组中删除项,然后使用jQuery重新序列化数组

取消序列化数据并从数组中删除项,然后使用jQuery重新序列化数组,jquery,arrays,serialization,Jquery,Arrays,Serialization,我正在尝试更新使用下面的php生成的序列化数组 // my php and array serialization $myArray = array( 161 => true, 180 => true ); $myData = serialize($myArray); setcookie('my_cookie', $myData); 我正在使用jQuery从我的cookie中获取这些序列化数据 // remove item click function $('.remove-ite

我正在尝试更新使用下面的php生成的序列化数组

// my php and array serialization
$myArray = array( 161 => true, 180 => true );
$myData = serialize($myArray);
setcookie('my_cookie', $myData);
我正在使用jQuery从我的cookie中获取这些序列化数据

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

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

});
这是我返回的序列化数据

a:2:{i:161;b:1;i:180;b:1;}
我希望能够取消序列化此数据并从数组中删除一项,然后再次序列化数据并重新设置cookie

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

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

});
因此,我将从我的
.remove item
按钮上的数据属性中获取该值,并从未序列化的数组中删除该值

它需要像这样工作,我只是不知道如何真正做到这一点。下面是我的演练,但这显然不起作用

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>
删除项目
任何想法或建议都将不胜感激


感谢

为了在客户机/服务器端之间处理数据,而不是使用序列化,我将其更改为JSON。这样,您只需运行
JSON.parse(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);

});
这应该能帮你解决:)