无法初始化和操纵二维会话阵列

时间:2013-06-05 09:17:23

标签: php session multidimensional-array

实际上我想将用户以前的搜索存储为会话作为二维数组 将查询分为关键字

<?php
session_start();

//weather searched or not
if(isset($_SESSION['searchedquery']))
{
    $string=$_SESSION['searchedquery'];
}
else
{
    $string="";
}

//when searched now query is made into key words



$encode = array(
    '/(\d+?)\.(\d+?)/' => '\\1DOT\\2',
    '/(\d+?),(\d+?)/' => '\\1COMMA\\2',
    '/(\d+?)-(\d+?)-(\d+?) (\d+?):(\d+?):(\d+?)/' => '\\1DASH\\2DASH\\3SPACE\\4COLON\\5COLON\\6'
);


foreach ($encode as $regex => $repl)
{
    $string = preg_replace($regex, $repl, $string);
}
preg_match_all('/\w+/', $string, $matches);
$decode = array(
                    'search' =>  array('DOT', 'COMMA', 'DASH', 'SPACE', 'COLON'),
                    'replace' => array('.',   ',',     '-',    ' ',     ':'    )
);
foreach ($matches as $k => $v)
{
    $matches[$k] = str_replace($decode['search'], $decode['replace'], $v);
}

//key words are obtained

// now session is maintained which captures the previus searches


/*
 number of previous searches done, here i'm saving a number of last
10 previous searches if this exceeds i'm using modulo operator to replace previous one by fifo/lifo ordeer
*/

if(!isset($_GET['searchednumber']))
{
    $_SESSION['searchednumber']=0;
}

// previous search value intialistalion //

/*from here actually i stucked , i want two dimentional array two keep track previous searched keywords */



if(!isset($_SESSION['searched']['values']))
{
    $values=Array("0","1","2","3","4","5","6","7","8","9","10");    // i'am unsure at this place
    $_SESSION['searched'][]=$values;            // i'am unsure at this place

}
$n=$_SESSION['searchednumber']; //total number of searches made

$p=$n%10;   // indices to store new query

if($n<10)   //number of iteration to done ... used just below this in for loop..
{
    $k=$n;
}
else
{
    $k=10;
}

$result=$matches[0];

//now preparing present query values from ablve ...top..
print_r($result);

//for debugging process

//code where i'm getting stucked dont why...



for($i=0; $i<$k; $i++)
{

    $result = array_udiff($result, $_SESSION['searched']['values'], 'strcasecmp');
}

//i'm unsure at this place
//here i want to clear all the keywords wchich were previously searched and to store in any one array i.e only allowing new keywords ... */


/*
 now depending upon result obtained where impli logic goes
*/

//but its not working as i intended its getting like ... "resource id #67" something is getting printed .....

print_r($result);

//now storing present query values to into indices...

$_SESSION['searched'][$p]=$matches[0];

?>

我被困在这个地方。

1 个答案:

答案 0 :(得分:0)

你的问题在

$_SESSION['searched']['values']

你在哪里

$_SESSION['searched'][]=$values;

所以它已被推送到数组,并且有一个int作为键,而不是'values'。