【问题标题】:php array_merge not working for cookies idphp array_merge 不适用于 cookie id
【发布时间】:2012-06-20 12:19:44
【问题描述】:

我的 php merge_array 有问题,我正在编写一个 cookie,它从 html 表单中的按钮获取元素 id,然后我创建一个 cookie setcookie("info", $_REQUEST["ELEMENT_ID"]+1, time ()+3600)。我想编写一个数组,将 $array1 与表单中的元素 id 和 $array2 合并,以获取 cookie 元素。当我点击页面上的购买按钮时出现问题,我总是在数组中有 2 个元素,新元素和 cookie 数组中的一个。 数组 ( [0] => [1] => 数组 ( [info] => 16 我正在寻找包含超过 2 个元素的数组 $result,以便我可以使用 id 将姓名、照片和其他属性放入购物车

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>
<?
$array1=array($_REQUEST["ELEMENT_ID"]);

if(!isset($_COOKIE["info"])){
    setcookie("info", $_REQUEST["ELEMENT_ID"]+1, time()+3600);
    $w = $_REQUEST["ELEMENT_ID"]+1;
    print_r($_COOKIE);
}
echo"<br/>";
$array2=array($_COOKIE);
$result= array_merge($array1, $array2);
print_r($result);

?>

【问题讨论】:

    标签: php cookies shopping-cart array-merge


    【解决方案1】:

    编辑:

    好的,现在我更好地理解了你想要做什么,这就是我的建议。由于您希望将历史数据存储在 cookie 中并且希望将其保存在一个数组中,因此您可以将数据存储为 cookie 中的序列化 id 数组。您现在要做的是获取当前的 ELEMENT_ID,向其中添加一个,然后将该值存储到 cookie 中,该 cookie 将覆盖已经存在的内容。所以我会用这个替换你所有的代码:

    <?php
        // do your checks
        if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();
    
        // 1: if cookie exists, grab the data out of it
        $historical_element_ids = array(); // initialize the variable as an array
        if(isset($_COOKIE['info'])){
            // retrieve the previous element ids as an array
            $historical_element_ids = unserialize($_COOKIE['info']);
        }
    
        // 2: add the new id to the list of ids (only if the id doesn't already exist)
        // the cookie will remain unchanged if the item already exists in the array of ids
        if(!in_array($_REQUEST['ELEMENT_ID'], $historical_element_ids)){
            $historical_element_ids[] = $_REQUEST['ELEMENT_ID']; // adds this to the end of the array
    
            // 3: set the cookie with the new serialized array of ids
            setcookie("info", serialize($historical_element_ids), time()+3600);
        }
    
        // display the cookie (should see a serialized array of ids)
        print_r($_COOKIE);
        echo"<br/>";
    
        // accessing the cookie's values
        $result = unserialize($_COOKIE['info']);
        print_r($result);
    ?>
    

    【讨论】:

    • 感谢 emmanuleG 的快速回复,但我得到了相同的结果,$result 数组中只有 2 个元素。我希望它像现在一样给我 $result 数组,其中包含超过 2 个元素,每当我单击购买按钮将 id 发送到 $_COOKIE 时,它应该继续添加到 $result 以便我有 1,2,3 ,4,5 等等元素,都是id。
    • 哦,好的,我相信我了解您想要做什么,但在更新我的答案之前我需要澄清一下。您希望将 ELEMENT_ID 的先前值存储在 cookie 中,以便在合并时获得迄今为止已提交的所有 ELEMENT_ID 的列表。是对的吗?因此,如果有人提交了一个 id 为 3 的项目并且你有 $_COOKIE = array('info'=>'1,5') 你想要 $result = array(3,1,5);对吗?
    • @jobkwemoi 根据您想要做的事情更新了我的答案。之前的问题是您每次都在覆盖 cookie 的值,因此您只会拥有当前的 ELEMENT_ID 和您存储在 cookie 中的最后一项。要解决此问题,您可以将 cookie 中的内容作为序列化数组存储,并通过反序列化、添加 id 并重新序列化以存储到 cookie 来更新它。
    • // 将此添加到数组的末尾,当您说我应该将其添加到数组的末尾时,这是否意味着我应该在末尾创建一个数组并将其添加为元素或只需将其添加为最后的代码行?
    • 非常感谢 EmmanuelG,就像魔术一样,现在我可以使用 id 获取产品名称和照片以在购物车中使用,谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多