我怎样才能得到所有以同一个单词开头的会话


How can I get all the session that start with the same word?

我在构建购物车时有问题。我怎么能让所有的会话都以同一个单词开头?

$start = 'startingword';
$new_session = array();
foreach($_SESSION as $index=>$value) {
    $temp = explode(' ', $index, 2);
    if($temp[0] == $start) {
        $new_session[$index] = $value;
    }
}

如果起始字是会话值而不是索引,则

foreach($_SESSION as $index=>$value) {
    $temp = explode(' ', $value, 2);
    if($temp[0] == $start) {
        $new_session[$index] = $value;
    }
}

从会话名称

的循环开始

foreach($_SESSION作为$string {)echo 'Session name is: '。美元的字符串。"
";}

这将回显所有会话名称然后修改代码,包括substr()来检查单词是否匹配-我不能真正给出这个代码,因为它取决于它是否总是相同的单词,在这种情况下,一个简单的substr()或可变长度,在这种情况下,先执行strlen("word")或字符分隔符-或_在这种情况下,你可以执行strpos();查找长度