array key unaccessable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

array key unaccessable?

Post by maliskoleather »

im storing a multi-dimensional array to a session from an unseraialized string... when i try and print out one of the arrays, it works great. but when i try and echo one of the indexes, it keeps giving me an undefined error.

for example:

Code: Select all

//store to session
//$string is just a serialized array being pulled from the database
$_SESSION['dirTreeStatus']['pages'] = unserialize($string);
then this is on the second page:

Code: Select all

//this works
print_r($_SESSION['dirTreeStatus']['pages']);

//this fails
echo $_SESSION['dirTreeStatus']['pages']['1'];
output is:

Code: Select all

Array ( [1] => OPEN [24] => OPEN [9] => OPEN )
Notice: Undefined index: 1 in /file.php on line 7
anyone have any idea whats going on?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

//try this
echo $_SESSION['dirTreeStatus']['pages'][1];
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

heh, that was one of my first thoughts.

that doesnt work either
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For giggles, use var_dump() instead of print_r().
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

that would be a:

Code: Select all

array(3) { ["1"]=>  string(4) "OPEN" ["24"]=>  string(4) "OPEN" ["9"]=>  string(4) "OPEN"}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look at the source code instead of what the browser is showing you. I suspect there's some HTML in there.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

nope. the source is the same:

Code: Select all

array(3) {
  ["1"]=>
  string(4) "OPEN"
  ["24"]=>
  string(4) "OPEN"
  ["9"]=>
  string(4) "OPEN"
}
the only thing i see thats sortof unusual is that the indexes are in double quotes, and i thought that normally in a var_dump they arent quoted at all.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

foreach($_SESSION['dirTreeStatus']['pages'] as $k=>$v) {
	echo "<pre>\n";
	var_dump($k);
	var_dump($v);
	var_dump($_SESSION['dirTreeStatus']['pages'][$k]);
	echo "</pre>\n";
}
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

volka wrote:please try

Code: Select all

foreach($_SESSION['dirTreeStatus']['pages'] as $k=>$v) {
	echo "<pre>\n";
	var_dump($k);
	var_dump($v);
	var_dump($_SESSION['dirTreeStatus']['pages'][$k]);
	echo "</pre>\n";
}
got this:

Code: Select all

string(1) "1"
string(4) "OPEN"


Notice:  Undefined index:  1 in /page.php on line 8

NULL

string(2) "24"
string(4) "OPEN"


Notice:  Undefined index:  24 in /page.php on line 8

NULL

string(1) "9"
string(4) "OPEN"


Notice:  Undefined index:  9 in /page.php on line 8

NULL
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

spooky.
May we see and test this magical string? :)
var_dump($string)
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

sure thing

Code: Select all

string(45) "a:3:{s:1:"1";s:4:"OPEN";s:2:"24";s:4:"OPEN";s:1:"9";s:4:"OPEN"}"
i doubt it actually matters, but just incase, i actually did this:

Code: Select all

var_dump(urldecode($string));
because my serialized string is being created by javascript, then ajax-ed the script that validates it and then stores it to the session. Either way, this is what gets unserialized.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Very strange.

Code: Select all

$s = 'a:3:{s:1:"1";s:4:"OPEN";s:2:"24";s:4:"OPEN";s:1:"9";s:4:"OPEN"}';
var_dump($s);
string(63) "a:3:{s:1:"1";s:4:"OPEN";s:2:"24";s:4:"OPEN";s:1:"9";s:4:"OPEN"}"
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

whoops. that was a typo on my part :oops: copied the wrong one, then didnt replace the whole thing.. :roll:

it's supposed to be

Code: Select all

string(63) "a:3:{s:1:"1";s:4:"OPEN";s:2:"24";s:4:"OPEN";s:1:"9";s:4:"OPEN"}"
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maliskoleather wrote:whoops. that was a typo on my part :oops: copied the wrong one, then didnt replace the whole thing.. :roll:
You mean you made up a fairy tale string? What else in the previous code isn't "the real thing"?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Maybe this has to do with the old session compatibility bug or the other hack in the core....
Post Reply