-1

我浏览了一百万个不同的网站,但似乎找不到答案——有没有办法从 cookie 中获取信息并将其放入数组中?我试过直接存储$_COOKIE['name']到一个数组中,这会导致服务器错误。我试过存储$_COOKIE['name']为 a $variable,但是当我打印出数组时,它实际上只是返回$variable. 成功做到这一点的人有什么智慧之言吗?

代码(除了实际的数组代码,整个页面都可以正常工作):

 $productarray = array("$_COOKIE['gatq']"=>'The Great Gatsby <img src="gatsby.jpg">',
                 "$_COOKIE['catcherq']"=>'Catcher in the Rye <img src="catcher.jpg">',
                  "$_COOKIE['wavesq']"=>'The Waves <img src="waves.jpg">',
                  "$_COOKIE['strangerq']"=>'The Stranger <img src="stranger.jpg">',
                  "$_COOKIE['ulyssesq']"=>'Ulysses <img src="ulysses.jpg">',
                  "$_COOKIE['prideq']"=>'Pride and Prejudice <img src="pride.jpg">',
                  "$_COOKIE['mockingbirdq']"=>'To Kill a Mockingbird <img src="mockingbird.jpg">',
                  "$_COOKIE['roadq']"=>'On the Road <img src="road.jpg">'
                  );

if ($_COOKIE['fname'] == NULL or $_COOKIE['lname'] == NULL or $_COOKIE['address'] == NULL or $_COOKIE['city'] == NULL or $_COOKIE['state'] == NULL or $_COOKIE['zip'] == NULL)
{echo "<script language='javascript'>
window.alert('You left some information on the personal info page! You will be redirected.');
    window.location.href='personal.php';</script>";
}

else
    {
        if ($_COOKIE['gatq'] == NULL &&
            $_COOKIE['catcherq'] == NULL &&
            $_COOKIE['wavesq'] == NULL &&
            $_COOKIE['strangerq'] == NULL &&
            $_COOKIE['ulyssesq'] == NULL &&
            $_COOKIE['prideq'] == NULL &&
            $_COOKIE['mockingbirdq'] == NULL &&
            $_COOKIE['roadq'] == NULL)

                {echo "<script language='javascript'>
                    window.alert('You don't have anything in your shopping cart! You will be redirected.');
                    window.location.href='inventory.php';</script>";
                }

                        else
                            {

                                asort($productarray);
                                foreach ($productarray as $book=>$info)
                                            {if ($book != NULL)
                                                    echo $book . "$info\n";
                                            }
                                echo $productarray;
                                echo "testing";


                            }

    }
?>

该错误只是说“服务器错误”,这与我遇到任何类型的语法错误时遇到的错误相同。

4

2 回答 2

2

变量不会在单引号字符串中展开。直接用作$_COOKIE['name']数组键即可。

$productarray = array($_COOKIE['gatq']=>'The Great Gatsby <img src="gatsby.jpg">',
                  $_COOKIE['catcherq']=>'Catcher in the Rye <img src="catcher.jpg">',
                  $_COOKIE['wavesq']=>'The Waves <img src="waves.jpg">',
                  $_COOKIE['strangerq']=>'The Stranger <img src="stranger.jpg">',
                  $_COOKIE['ulyssesq']=>'Ulysses <img src="ulysses.jpg">',
                  $_COOKIE['prideq']=>'Pride and Prejudice <img src="pride.jpg">',
                  $_COOKIE['mockingbirdq']=>'To Kill a Mockingbird <img src="mockingbird.jpg">',
                  $_COOKIE['roadq']=>'On the Road <img src="road.jpg">'
                  );

不过,我不确定您在这里真正想要做什么。为什么 cookie 的值会成为关联数组的键?$_COOKIE['gatq']例如,里面有什么?

我认为你真正想要的是一个二维数组:

$productarray = array(
                  array('quantity'=>$_COOKIE['gatq'], 'title'=>'The Great Gatsby <img src="gatsby.jpg">'),
                  array('quantity'=>$_COOKIE['catcherq'], 'title'=>'Catcher in the Rye <img src="catcher.jpg">'),
                  array('quantity'=>$_COOKIE['wavesq'], 'title'=>'The Waves <img src="waves.jpg">'),
                  array('quantity'=>$_COOKIE['strangerq'], 'title'=>'The Stranger <img src="stranger.jpg">'),
                  array('quantity'=>$_COOKIE['ulyssesq'], 'title'=>'Ulysses <img src="ulysses.jpg">'),
                  array('quantity'=>$_COOKIE['prideq'], 'title'=>'Pride and Prejudice <img src="pride.jpg">'),
                  array('quantity'=>$_COOKIE['mockingbirdq'], 'title'=>'To Kill a Mockingbird <img src="mockingbird.jpg">'),
                  array('quantity'=>$_COOKIE['roadq'], 'title'=>'On the Road <img src="road.jpg">')
                  );

要按数量顺序显示,您可以使用usort()比较的功能$element['quantity']

于 2013-05-02T05:27:01.090 回答
0

$_COOKIE已经是一个数组。

print_r($_COOKIE);

您可以像处理任何其他数组一样处理它。

if(!empty($_COOKIE['catcherq'])){
  $catcherq = $_COOKIE['catcherq'];
  echo $catcherq; 
}

foreach($_COOKIE as $key=>$value){
  echo ("$key:$value\n");
  ${$key}=$value;
}
于 2013-05-02T05:26:20.070 回答