Home > Backend Development > PHP Tutorial > SESSION存多维数组问题

SESSION存多维数组问题

WBOY
Release: 2016-06-23 13:28:25
Original
1205 people have browsed it

需求是把最近访问的分类ID(3个)存入到SESSION里 

然后通过循环去数据库里取对应的分类名称

我是先把得到的3个分类id拼成一个数组 存到session里 
然后再把它取出来 再压到那个数组里
现在问题是取出来的数据并不是我想要的一个二维数组 
这是我的代码:

session_start();$cate1 = $_GET['cate1'];$cate2 = $_GET['cate2'];$cate3 = $_GET['cate3'];$data[] = array('cate1'=>$cate1,'cate2'=>$cate2,'cate3'=>$cate3);if($_SESSION['cate']){	$data[] = $_SESSION['cate'];}$_SESSION['cate'] = $data;
Copy after login


这是第三次访问 返回的数据:
array(2) {  [0]=>  array(3) {    ["cate1"]=>    string(2) "35"    ["cate2"]=>    string(2) "43"    ["cate3"]=>    string(2) "12"  }  [1]=>  array(2) {    [0]=>    array(3) {      ["cate1"]=>      string(2) "35"      ["cate2"]=>      string(2) "43"      ["cate3"]=>      string(2) "12"    }    [1]=>    array(2) {      [0]=>      array(3) {        ["cate1"]=>        string(2) "95"        ["cate2"]=>        string(2) "64"        ["cate3"]=>        string(2) "13"      }      [1]=>      array(2) {        [0]=>        array(3) {          ["cate1"]=>          string(2) "95"          ["cate2"]=>          string(2) "64"          ["cate3"]=>          string(2) "13"        }        [1]=>        array(1) {          [0]=>          array(3) {            ["cate1"]=>            string(2) "55"            ["cate2"]=>            string(2) "24"            ["cate3"]=>            string(2) "43"          }        }      }    }  }}
Copy after login

这个结果让我很伤心 如果才能得到一个正常的二维数组


回复讨论(解决方案)

session_start();$cate1 = $_GET['cate1'];$cate2 = $_GET['cate2'];$cate3 = $_GET['cate3'];$data = array('cate1'=>$cate1,'cate2'=>$cate2,'cate3'=>$cate3);if($_SESSION['cate']){    $data = $_SESSION['cate'];} $_SESSION['cate'] = $data;
Copy after login

这样也不行啊 版主大哥 这样取到的永远只有一个一维数组

http://localhost/test?cate1=23&cate2=34&cate3=45
http://localhost/test?cate1=24&cate2=35&cate3=46
我请求了两次它返回的都是这个数据

array(3) {  ["cate1"]=>  string(2) "23"  ["cate2"]=>  string(2) "34"  ["cate3"]=>  string(2) "45"}
Copy after login


我希望是这样的
array(3) {[0]=>{    ["cate1"]=>  string(2) "23"  ["cate2"]=>  string(2) "34"  ["cate3"]=>  string(2) "45"}[1]=>{    ["cate1"]=>  string(2) "24"  ["cate2"]=>  string(2) "35"  ["cate3"]=>  string(2) "46"}}
Copy after login


谢谢回复 麻烦再看下

session_start();if(! isset($_SESSION['cate']) ) $_SESSION['cate'] = array();$cate1 = $_GET['cate1'];$cate2 = $_GET['cate2'];$cate3 = $_GET['cate3'];$_SESSION['cate'][] = array('cate1'=>$cate1,'cate2'=>$cate2,'cate3'=>$cate3);
Copy after login

靠 原来这么简单 ……。 想太复杂了 谢谢版主

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template