如何使用正则表达式从数组中查找匹配键

时间:2013-08-17 06:10:17

标签: php arrays session joomla

当我打印我的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
        )

)

有任何想法或建议吗?感谢。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)