数组中的唯一条目 [英] Unique entries in an array

查看:113
本文介绍了数组中的唯一条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储previous 10的URL进入会议的情况如下:

I have the following that stores the previous 10 URL's into a session:

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
//Insert Current URL in SESSION
       $CurrentPage = curPageURL();
       if(strpos($CurrentPage, '/products/'))
    	{
    	echo "<div class=\"title\">Recently viewed products</div>
<div id=\"recent\">";
       $_SESSION['pages'][] = $CurrentPage;
       if ( Count ( $_SESSION['pages'] ) > 10 )
    	Array_Shift ( $_SESSION['pages'] );

我如何确保只有唯一条目存储?

How do I make sure only unique entries are stored?

谢谢,

推荐答案

就在

$_SESSION['pages'][] = $CurrentPage;

您需要添加

$_SESSION['pages'] = array_unique($_SESSION['pages']);

文档都可以这里

此方法需要较少的处理,因为它是一个原生的功能。执行如果在阵列中的每个项目可能是相当昂贵的。

This method requires less processing, as it's a native function. Performing an 'if' on each item in the array could potentially be quite costly.

这篇关于数组中的唯一条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆