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
maliskoleather
Forum Contributor
Posts: 155 Joined: Tue May 15, 2007 2:19 am
Contact:
Post
by maliskoleather » Tue Sep 18, 2007 4:58 pm
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?
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Tue Sep 18, 2007 5:14 pm
Code: Select all
//try this
echo $_SESSION['dirTreeStatus']['pages'][1];
maliskoleather
Forum Contributor
Posts: 155 Joined: Tue May 15, 2007 2:19 am
Contact:
Post
by maliskoleather » Tue Sep 18, 2007 5:19 pm
heh, that was one of my first thoughts.
that doesnt work either
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 18, 2007 5:32 pm
For giggles, use
var_dump() instead of
print_r() .
maliskoleather
Forum Contributor
Posts: 155 Joined: Tue May 15, 2007 2:19 am
Contact:
Post
by maliskoleather » Tue Sep 18, 2007 6:00 pm
that would be a:
Code: Select all
array(3) { ["1"]=> string(4) "OPEN" ["24"]=> string(4) "OPEN" ["9"]=> string(4) "OPEN"}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 18, 2007 6:23 pm
Look at the source code instead of what the browser is showing you. I suspect there's some HTML in there.
maliskoleather
Forum Contributor
Posts: 155 Joined: Tue May 15, 2007 2:19 am
Contact:
Post
by maliskoleather » Tue Sep 18, 2007 6:28 pm
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.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Sep 18, 2007 6:29 pm
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";
}
maliskoleather
Forum Contributor
Posts: 155 Joined: Tue May 15, 2007 2:19 am
Contact:
Post
by maliskoleather » Tue Sep 18, 2007 6:34 pm
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Sep 18, 2007 6:36 pm
spooky.
May we see and test this magical string?
var_dump($string)
maliskoleather
Forum Contributor
Posts: 155 Joined: Tue May 15, 2007 2:19 am
Contact:
Post
by maliskoleather » Tue Sep 18, 2007 6:46 pm
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:
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.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Sep 18, 2007 6:52 pm
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"}"
maliskoleather
Forum Contributor
Posts: 155 Joined: Tue May 15, 2007 2:19 am
Contact:
Post
by maliskoleather » Tue Sep 18, 2007 6:57 pm
whoops. that was a typo on my part
copied the wrong one, then didnt replace the whole thing..
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"}"
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Sep 18, 2007 6:58 pm
maliskoleather wrote: whoops. that was a typo on my part
copied the wrong one, then didnt replace the whole thing..
You mean you made up a fairy tale string? What else in the previous code isn't "the real thing"?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 18, 2007 7:00 pm
Maybe this has to do with the old session compatibility bug or the other hack in the core....