在 PHP cookie 中存储和检索数组

编程入门 行业动态 更新时间:2024-10-11 17:23:00
本文介绍了在 PHP cookie 中存储和检索数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从一些虚拟"索引卡中存储一些数据.每张卡片都有正面和背面,用户可以存放多张卡片.每一方都会有数据.

I ----------------- I I CARD 1 FRONT I -----------------II --------------- I I CARD 1 BACK I -----------------II ----------------- I I CARD 2 FRONT I -----------------II --------------- I I CARD 2 BACK I -----------------I

好吧,我的图表有点乱.但是你会收到消息.:)

从上图中想象一下.我想将每张卡片的数据(正面和背面)存储在 cookie 中,作为一个数组(也许),然后能够将每个值拉回并在适用的地方(在不同的页面上)插入.

同时请记住,用户可以制作任意数量的卡片.我不能使用 POST 或 GET 函数.数组位是有争议的,如果您能想到一种更简单的方法将这些数据存储在 cookie 中,请告诉我.请注意:不建议存储在数据库中,因为它对项目不方便.:)

解决方案

使用 json_encode/json_decode 来获取/设置 cookie 中的数组.

测试数组

$cardArray=array('CARD 1'=>array('FRONT I', 'BACK I'),'卡 2'=> 数组('前 2','后 2'));

转换并写入cookie

$json = json_encode($cardArray);setcookie('cards', $json);

保存的字符串看起来像这样

{"CARD 1":["FRONT I","BACK I"],"CARD 2":["FRONT 2","BACK 2"]}

取回饼干

$cookie = $_COOKIE['cards'];$cookie = stripslashes($cookie);$savedCardArray = json_decode($cookie, true);

显示恢复后的数组

echo '

';打印_r($savedCardArray);echo '</pre>';

输出

数组([卡片 1] =>大批([0] =>前我[1] =>返回我)[卡片 2] =>大批([0] =>前 2[1] =>返回 2))

编辑如果你对stripslashes感到疑惑,那是因为实际保存的字符串是

{\"CARD 1\":[\"FRONT I\",\"BACK I\"],\"CARD 2\":[\"FRONT 2\",\"BACK 2\"]}

setcookie 在引号前添加 \ 来转义它们.如果你不摆脱这些,json_decode 将会失败.

编辑二

向 cookie 添加一张新卡片

  • 如上加载数组
  • $savedCardArray['CARD XX']=array('FRONT XX', 'BACK XX');
  • 如上保存数组,但现在当然是$savedCardArray 而不是$cardArray.
  • I'm looking to store some data from some 'virtual' index cards. Each card has a front and a back, and the user can store multiple cards. Each side will have data on it.

    I ----------------- I I CARD 1 FRONT I I------------------I I --------------- I I CARD 1 BACK I I-----------------I I ----------------- I I CARD 2 FRONT I I------------------I I --------------- I I CARD 2 BACK I I-----------------I

    OK, my diagrams got messed up a bit. But you get the message. :)

    Imagine it from the diagrams above. I'd like to store each card's data (front and back) in a cookie, as an array (maybe), and then be able to pull each value back and insert it where applicable (on a different page).

    At the same time, bear in mind that the user can make as many cards as they like. I can't use POST or GET functions. The array bit is debatable, if you can think of an easier way of storing this data in a cookie, let me know. Please note: don't suggest storing in a database, as it won't be convenient for the project. :)

    解决方案

    Use json_encode / json_decode to get / set arrays in cookies.

    Test array

    $cardArray=array( 'CARD 1'=>array('FRONT I', 'BACK I'), 'CARD 2'=>array('FRONT 2', 'BACK 2') );

    convert and write the cookie

    $json = json_encode($cardArray); setcookie('cards', $json);

    the saved string looks like this

    {"CARD 1":["FRONT I","BACK I"],"CARD 2":["FRONT 2","BACK 2"]}

    get the cookie back

    $cookie = $_COOKIE['cards']; $cookie = stripslashes($cookie); $savedCardArray = json_decode($cookie, true);

    show the restored array

    echo '<pre>'; print_r($savedCardArray); echo '</pre>';

    outputs

    Array ( [CARD 1] => Array ( [0] => FRONT I [1] => BACK I ) [CARD 2] => Array ( [0] => FRONT 2 [1] => BACK 2 ) )

    Edit If you wonder about stripslashes, it is because the string saved actually is

    {\"CARD 1\":[\"FRONT I\",\"BACK I\"],\"CARD 2\":[\"FRONT 2\",\"BACK 2\"]}

    setcookie adds \ before quoutes to escape them. If you not get rid of those, json_decode will fail.

    Edit II

    To add a new card to the cookie

  • load the array as above
  • $savedCardArray['CARD XX']=array('FRONT XX', 'BACK XX');
  • save the array as above, but now of course $savedCardArray and not $cardArray.
  • 更多推荐

    在 PHP cookie 中存储和检索数组

    本文发布于:2023-05-29 03:39:13,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/335902.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!