MaurerPower
MaurerPower

Reputation: 2054

php session variable multidimensional associative array issue

I've looked around SO, but can't find an explanation to what is going on in my $_SESSION variables.

@ob_start();
$k=@ob_get_contents();
@ob_end_clean();
@session_start();
unset($s,$m);
$m1 = explode(" ", microtime());
$stime = $m1[1] + $m1[0];
echo $k;

$_SESSION['resendConfirmation']['function'] = 'resend';
$_SESSION['resendConfirmation']['id'] = '8';                

print_r($_SESSION);

outputs:

Array ( [resendConfirmation] => 8esend ) 

Why is it string replacing? I've never had this issue before.

What I want is thus:

Array([resendConfirmation] => Array(
                             [id] =>8
                             [function} => resend
                             )
)

I've never had this happen before, I'm totally confused!

UPDATE In response to @DanRedux I've changed to two non-existent variable names to take the referencing out of the equation, still the same result...

$_SESSION['resendConfirmation']['tweak'] = 'resend';
$_SESSION['resendConfirmation']['tweak2'] = '8';

Same result :(

Did a sitewide query of resendConfirmation and none were found, but once I change that array name, it all worked, baffled, but fixed...

$_SESSION['reConfirm']['function'] = 'resend';
$_SESSION['reConfirm']['id'] = '8';             

print_r($_SESSION);

Upvotes: 2

Views: 5108

Answers (2)

PeeHaa
PeeHaa

Reputation: 72672

What you think is an multidimensional array really isn't. What really happens is:

What you think is an array is really a string. After that you are trying to access the string as an array. You are trying to access the element id which doesn't exists. PHP always tries to be smarter than it should and just says: OK I'll assume you meant the first index. So basically what happens is:

<?php
$notAnArray = 'somestring';
$notAnArray['id'] = '8'; 

var_dump($notAnArray); // 8omestring

This is the reason you should always enable error_reporting on your development machine:

error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);

And never suppress errors using @. Well there are some situations where you can use @, but this really isn't one of them.

Upvotes: 6

EmmanuelG
EmmanuelG

Reputation: 1051

Since I dont really know what other sorts of shenanigans the code is up to outside of this block you gave us I would say to just try this instead:

$_SESSION['resendConfirmation'] = array('id' => 8, 'function' => 'resend');

If this also fails then there has to be something else going on outside of what you posted. Good luck!

Upvotes: 6

Related Questions