Page 1 of 2

array key unaccessable?

Posted: Tue Sep 18, 2007 4:58 pm
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?

Posted: Tue Sep 18, 2007 5:14 pm
by Zoxive

Code: Select all

//try this
echo $_SESSION['dirTreeStatus']['pages'][1];

Posted: Tue Sep 18, 2007 5:19 pm
by maliskoleather
heh, that was one of my first thoughts.

that doesnt work either

Posted: Tue Sep 18, 2007 5:32 pm
by feyd
For giggles, use var_dump() instead of print_r().

Posted: Tue Sep 18, 2007 6:00 pm
by maliskoleather
that would be a:

Code: Select all

array(3) { ["1"]=>  string(4) "OPEN" ["24"]=>  string(4) "OPEN" ["9"]=>  string(4) "OPEN"}

Posted: Tue Sep 18, 2007 6:23 pm
by feyd
Look at the source code instead of what the browser is showing you. I suspect there's some HTML in there.

Posted: Tue Sep 18, 2007 6:28 pm
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.

Posted: Tue Sep 18, 2007 6:29 pm
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";
}

Posted: Tue Sep 18, 2007 6:34 pm
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

Posted: Tue Sep 18, 2007 6:36 pm
by volka
spooky.
May we see and test this magical string? :)
var_dump($string)

Posted: Tue Sep 18, 2007 6:46 pm
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.

Posted: Tue Sep 18, 2007 6:52 pm
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"}"

Posted: Tue Sep 18, 2007 6:57 pm
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"}"

Posted: Tue Sep 18, 2007 6:58 pm
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"?

Posted: Tue Sep 18, 2007 7:00 pm
by feyd
Maybe this has to do with the old session compatibility bug or the other hack in the core....