4

当我打印我的 Joomla 会话时,我得到如下的数组输出。现在我想找到compare_session并且所有使用这个键的 mtaching 都需要用逗号分开存储。我不会使用重复的键形式数组。

我想要这样的输出:

281,147,136,96,46,30

Array
(
    [__default] => Array
        (
            [session.counter] => 9
            [session.timer.start] => 1376717892
            [session.timer.last] => 1376718229
            [session.timer.now] => 1376718247
            [session.client.browser] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
            [registry] => JRegistry Object
                (
                    [data:protected] => stdClass Object
                        (
                        )

                )

            [user] => JUser Object
                (
                    [isRoot:protected] => 1
                    [id] => 42
                    [name] => Manan
                    [username] => Manan
                    [email] => 
 manan@letsnurture.com
                    [password] => 3c4ebaabb8701e80c581c8a291c44073:Aegdm5ddVjEhkJjPIdFlLqj6QZCRnWFO
                    [password_clear] => 
                    [usertype] => deprecated
                    [block] => 0
                    [sendEmail] => 1
                    [registerDate] => 2012-10-15 02:03:41
                    [lastvisitDate] => 2013-08-17 05:38:12
                    [activation] => 0
                    [params] => {"admin_style":"","admin_language":"","language":"","editor":"","helpsite":"","timezone":""}
                    [groups] => Array
                        (
                            [8] => 8
                        )

                    [guest] => 
                    [lastResetTime] => 0000-00-00 00:00:00
                    [resetCount] => 0
                    [_params:protected] => JRegistry Object
                        (
                            [data:protected] => stdClass Object
                                (
                                    [admin_style] => 
                                    [admin_language] => 
                                    [language] => 
                                    [editor] => 
                                    [helpsite] => 
                                    [timezone] => 
                                )

                        )

                    [_authGroups:protected] => Array
                        (
                            [0] => 1
                            [1] => 8
                        )

                    [_authLevels:protected] => Array
                        (
                            [0] => 1
                            [1] => 1
                            [2] => 2
                            [3] => 3
                        )

                    [_authActions:protected] => 
                    [_errorMsg:protected] => 
                    [_errors:protected] => Array
                        (
                        )

                )

            [session.token] => 0d79f30e557acf79cada5e55d837caf8
            [breadcrumbs] => http://letsnurture-php/PropertyFindbeta/my-propertyfind/watch-list.html?page=2
            [pageURL] => http://letsnurture-php/PropertyFindbeta/compare-items.html
            [flag] => true
            [compare_session_20130817110918] => 281,147,136
            [compare_session_20130817111010] => 96,46,30
            [compare_session_20130817111333] => 96,46,30
            [compare_session_20130817111349] => 96,46,30
            [compare_session_20130817111408] => 96,46,30
        )

)

有什么想法或建议吗?谢谢。

4

2 回答 2

5
$sessions = array();
foreach ($mySession['__default'] as $key => $value)
{
    if (preg_match("/^compare_session_(.*)$/", $key))
    {
        $exp = explode(",", $value);
        foreach ($exp as $session)
        {
            if (!in_array($session, $sessions))
                $sessions[] = $session;
        }
    }
}

var_dump($sessions);

输出

array (size=6)
  0 => string '281' (length=3)
  1 => string '147' (length=3)
  2 => string '136' (length=3)
  3 => string '96' (length=2)
  4 => string '46' (length=2)
  5 => string '30' (length=2)
于 2013-08-17T06:26:55.453 回答
1
foreach($array as $key=>$value)
{
   preg_math('compare_session_(.*?)',$key,$mathch);
   if($match[1] AND in_array($value,$out)=== false)
   {
      $out[] = $value;
   }
}

var_dump($out)
于 2013-08-17T06:16:15.253 回答