Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4038

cannot store in multidimensional session array

If I save a string in $_SESSION['breadcrumb']['category'] it successfully saves the string , but if I try to save the same string in $_SESSION['breadcrumb']['category']['shiv'] it throws the following error

Warning: Illegal string offset 'shiv' in E:\wamp\www\sugumar\mysuite\ajaxjobsearch.php on line 13

this is working

 $_SESSION['breadcrumb']['category']="<a href={$sitepath}/jobs?removefromsearch&name=category&id={$id}>$value</a>" ;//works fine

this is not working..

$_SESSION['breadcrumb']['category']['shiv'] ="<a href={$sitepath}/jobs?removefromsearch&name=category&id={$id}>$value</a>" ; //error

I want to save $id instead of shiv

$_SESSION['breadcrumb']['category'][$id] ="<a href={$sitepath}/jobs?removefromsearch&name=category&id={$id}>$value</a>" ;

Upvotes: 0

Views: 87

Answers (1)

mickmackusa
mickmackusa

Reputation: 47894

Because your deep category value is first a string, when you then try to access it using [anything] it is trying to access an offset of that string. Here is an example:

$_SESSION['breadcrumb']['category']='<a href={$sitepath}/jobs?removefromsearch&name=category&id={$id}>$value</a>';
echo $_SESSION['breadcrumb']['category'][0];

This will output < because that is the character at offset 0 (first character).

Truth is shiv isn't a legal offset, so php will bang out a warning, and then desperately try to do what you asked it to... it will convert shiv to 0 and replace the first character of character's value < with the first character of shiv's value < (no noticeable change)


When the offset is a valid number, you can use it to replace a character in the string:

$_SESSION['breadcrumb']['category']='<a href={$sitepath}/jobs?removefromsearch&name=category&id={$id}>$value</a>';//works fine
$_SESSION['breadcrumb']['category'][0]='!';
echo $_SESSION['breadcrumb']['category'];

Output: !a href={$sitepath}/jobs?removefromsearch&name=category&id={$id}>$value</a>


That should sufficiently explain the error.

As others have stated, this is just a matter of forcing a round peg into a square hole. To correct the issue, just declare a key after ['category'] in your first posted line of code so that category is the key to a subarray.

Upvotes: 2

Related Questions