数组数据如何存储在会话中

时间:2013-07-24 10:18:01

标签: php arrays session

我在回收存储在会话中的数组时遇到了挑战。 我将数组存储在像这样的数组中

$downlines = '2,3,4,5,6,7,8';
$_SESSION['downline'] = $afrisoft->dbarray("SELECT username, email FROM users WHERE id IN $downlines")

当我打印会话时,我得到了这个

Array (
      [0] => Array (
                   [username] => mcbel 
                   [email] => firstmail@gmail.com
                   )
      [1] => Array (
                   [username] => bimibola
                   [email] => secondmail@yahoo.com
                   )
      [2] => Array (
                   [username] => shadie
                   [email] => thirdmail@gmail.com
                   )
      [3] => Array (
                   [username] => Hifee
                   [email] => ife@ife.net
                   )
      )

我打算实现的目标是将数据存储在['usernmae']['email']中,但是当我尝试print_r($_SESSION['downline']['username'])print_r($_SESSION['downline']['email'])时,它不返回任何值。

我会感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您的函数返回了一个数组。您可以通过$_SESSION['downline'][0]['username']

访问该数组的第一个元素

您可以通过

遍历$_SESSION['downline']来访问完整结果
foreach ($_SESSION['downline'] as $item) {
   echo $item['username']
} 

答案 1 :(得分:1)

<?php

foreach($_SESSION['downline'] as $userDetails){
    echo $userDetails['username'];
}