dougan0529
2012-05-10 19:09
浏览 47
已采纳

php会话变量多维关联数组问题

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);

图片转代码服务由CSDN问答提供 功能建议

我环顾四周,但无法找到我的$ _SESSION变量中发生的事情的解释

 <代码> @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'] ='重新发送'; 
 $ _SESSION ['resendConfirmation'] ['id'] ='8';  
 
print_r($ _ SESSION); 
   
 
 

输出:

  Array([resendConfirmation] =&gt;  8esend)
   
 
 

为什么要替换字符串? 我以前从未遇到过这个问题。

我想要的是:

  Array([resendConfirmation] =&gt;数组(
 [id] =&gt; 8  
 [function} =&gt;重新发送
)
)
   
 
 

我以前从来没有遇到过这种情况,我完全糊涂了!

更新 为响应@DanRedux,我已更改为两个不存在的变量名称,以便将参考值排除在等式之外 相同的结果...

  $ _ SESSION ['resendConfirmation'] ['tweak'] ='重新发送'; 
 $ _SESSION ['resendConfirmation'] ['tweak2'  ] ='8'; 
   
 
 

同样的结果:(

resendConfirmation 并且没有找到,但是一旦我更改了这个数组名称,它一切都有效,令人困惑,但已修复......

  $ _ SESSION ['reConfirm'] ['function'  ] ='重新发送'; 
 $ _SESSION ['reConfirm'] ['id'] ='8'; 
 
print_r($ _ SESSION); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchuli5647 2012-05-10 19:22
    已采纳

    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!

    已采纳该答案
    打赏 评论
  • doushan3511 2012-05-10 19:21

    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.

    打赏 评论

相关推荐 更多相似问题