ucenter我想有不少朋友会用到了,就是一个会员同步功能了,我们经常会把几个系统或不同论坛整合起来,下面我来给各位同学介绍一下。
UCenter同步流程:
前提是需要在Ucenter上面添加好需要同步登录的应用(至少2个才能看到效果),并且显示:通信成功
假如我添加了A,B两个应用网站
1.首先当A站登录时,登录成功后,处理实质是调用uc_client提供的方法向UCenter获取2个脚本代码(A、B站),这两个脚本代码就是访问A、B两个站的api/uc.php中的登录方法,可以在方法中做登录所需session和cookie操作。
2.当登出时,操作和登录是一样的,都需要向UCenter获取2个脚本代码,目的是用来触发A、B站的api/uc.php中登出方法。
重点:当获取到2个脚本代码后一定需要在输出让浏览器运行,否则将不会实现同步登录登出。
建议:开发调试的时候,借助浏览器监控来查看返回输出值,我当时用的是chrome F12 后 打开Network标签 。
实例详解
所谓单点登录,无非就是几个站点共用一个用户中心,实现同步登陆,同步退出。
服务器端:Loog SSO
客服端: ucenter,说实话dz商业化确实让php发展了不少。
ucenter 基本原理:
其实最终还是 用户去登录 只是采用了ajax 用户不会发现。
我们来看看和ucenter的具体程序:
config.php [PHP代码]:
代码如下 |
复制代码 |
define(‘UC_CONNECT’, ’mysql’); // 连接 UCenter 的方式: mysql/NULL, 默认为空时为fscoketopen()
//数据库相关 (mysql 连接时, 并且没有设置 UC_DBLINK 时, 需要配置以下变量)
define(‘UC_DBHOST’, ’localhost’); // UCenter 数据库主机
define(‘UC_DBUSER’, ’root’); // UCenter 数据库用户名
define(‘UC_DBPW’, ’123′); // UCenter 数据库密码
define(‘UC_DBNAME’, ’ucenter’); // UCenter 数据库名称
define(‘UC_DBCHARSET’, ’utf8′); // UCenter 数据库字符集
define(‘UC_DBTABLEPRE’, ’ucenter.uc_’); // UCenter 数据库表前缀
define(‘UC_KEY’, ’safefewfef’); // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
define(‘UC_API’, ’http://www.111cn.net/uc’);// UCenter 的 URL 地址, 在调用头像时依赖此常量
define(‘UC_CHARSET’, ’utf-8′); // UCenter 的字符集
define(‘UC_IP’, ’127.0.0.1′); // UCenter 的 IP, 当 UC_CONNECT 为非 mysql 方式时, 并且当前应用服务器解析域名有问题时, 请设置此值
define(‘UC_APPID’, ’3′); // 对应到ucenter当前应用的 ID
define(‘UCDOMAIN’,'http://www.111cn.net/’); // 域名设置
//一些 Cookie 设置
$_UC = array();
$_UC["cookiedomain"] = ”; // cookie 作用域
$_UC["cookiepath"] = ’/'; // cookie 作用路径
$_UC["cookiepre"] = ’uc_’; // cookie 前缀
$_UC["cookietime"] = ’31536000′; //cookie 作用时间
|
配置文件写好后,到ucenter后台里面添加一个应用记住是自定义的 ‘UC_KEY’必须和config.php里面相同
接下来就是你的主目录下的api/uc.php
例如应用url 填写为 http://www.111cn.net 那么我就有对应的http://www.111cn.net/api/uc.php
如果要自定义的话 请确认你的对应关系。
最重要的就是api/uc.php ,同步登录就是访问各个应用的uc.php 关于这个dz已经给了demo
[PHP代码]:
代码如下 |
复制代码 |
define(‘API_DELETEUSER’,0); //note 用户删除 API 接口开关
define(‘API_RENAMEUSER’, 0); //note 用户改名 API 接口开关
define(‘API_GETTAG’, 0); //note 获取标签 API 接口开关
define(‘API_SYNLOGIN’, 1); //note 同步登录 API 接口开关
define(‘API_SYNLOGOUT’, 1); //note 同步登出 API 接口开关
define(‘API_UPDATEPW’, 0); //note 更改用户密码 开关
define(‘API_UPDATEBADWORDS’, 0); //note 更新关键字列表 开关
define(‘API_UPDATEHOSTS’, 0); //note 更新域名解析缓存 开关
define(‘API_UPDATEAPPS’, 0); //note 更新应用列表 开关
define(‘API_UPDATECLIENT’, 0); //note 更新客户端缓存 开关
define(‘API_UPDATECREDIT’, 0); //note 更新用户积分 开关
define(‘API_GETCREDITSETTINGS’, 0); //note 向 UCenter 提供积分设置 开关
define(‘API_GETCREDIT’,0); //note 获取用户的某项积分 开关
define(‘API_UPDATECREDITSETTINGS’, 0); //note 更新应用积分设置 开关
|
这些参数都是向别的应用提供的功能开关
最后关于 自己的页面如何同步登录 别的应用
[PHP代码]:
代码如下 |
复制代码 |
include_once ’../config.php’;
include_once ’../uc_client/client.php’;
|
你的验证登录部分
代码如下 |
复制代码 |
list($uid, $username, $password) = uc_user_login($_POST[username], $_POST[password]);//进入ucenter验证
$ucsynlogin = uc_user_synlogin($uid);//同步登录
echo $ucsynlogin;//因为是ajax 要echo
|
只用php,康盛的解决方案是比较不错的了,而且利用了p3p头实现了 不同域名 单点登录
缺点就是采用ajax 客服端请求 ,如果有10个以上应用,登录速度就慢下来了,这时候就可以考虑下七夜的Loong SSO
了解了以上的东东 php的CMS和ucenter通信 就不难了。
在php中我们要做伪静态可以直接利用php来处理url,但这种伪静态不是标准的可以说看起来像水货一样的,只是找个心里舒服了,当然如果要实现真正的伪静态我们可以使用apache Rewrite伪静态模块来实例,下面大家来看看。
php程序实现伪静态url如下。
代码如下 |
复制代码 |
<?php
//伪静态方法一
// localhost/php100/test.php?id|1@action|2
$Php2Html_FileUrl = $_SERVER["REQUEST_URI"];
echo $Php2Html_FileUrl."<br>";// /php100/test.php?id|1@action|2
$Php2Html_UrlString = str_replace("?","",str_replace("/", "", strrchr(strrchr($Php2Html_FileUrl, "/"),"?")));
echo $Php2Html_UrlString."<br>";// id|1@action|2
$Php2Html_UrlQueryStrList = explode("@", $Php2Html_UrlString);
print_r($Php2Html_UrlQueryStrList);// Array ( [0] => id|1 [1] => action|2 )
echo "<br>";
foreach($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr)
{
$Php2Html_TmpArray = explode("|", $Php2Html_UrlQueryStr);
print_r($Php2Html_TmpArray);// Array ( [0] => id [1] => 1 ) ; Array ( [0] => action [1] => 2 )
echo "<br>";
$_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1];
}
//echo '假静态:$_GET变量<br />';
print_r($_GET); // Array ( [id|1@action|2] => [id] => 1 [action] => 2 )
echo "<br>";
echo "<hr>";
echo $_GET[id]."<br>";// 1
echo $_GET[action];// 2
?>
<?php
//伪静态方法二
// localhost/php100/test.php/1/2
$filename = basename($_SERVER['SCRIPT_NAME']);
echo $_SERVER['SCRIPT_NAME']."<br>";// /php100/test.php
echo $filename."<br>";// test.php
if(strtolower($filename)=='test.php'){
if(!empty($_GET[id])){
$id=intval($_GET[id]);
echo $id."<br>";
$action=intval($_GET[action]);
echo $action."<br>";
}else{
$nav=$_SERVER['REQUEST_URI'];
echo "1:".$nav."<br>";// /php100/test.php/1/2
$script=$_SERVER['SCRIPT_NAME'];
echo "2:".$script."<br>";// /php100/test.php
$nav=ereg_replace("^$script","",urldecode($nav));
echo $nav."<br>"; // /1/2
$vars=explode("/",$nav);
print_r($vars);// Array ( [0] => [1] => 1 [2] => 2 )
echo "<br>";
$id=intval($vars[1]);
$action=intval($vars[2]);
}
echo $id.'&'.$action;
}
?>
<?php
//伪静态方法三
function mod_rewrite(){
global $_GET;
$nav=$_SERVER["REQUEST_URI"];
echo $nav."<br>";
$script_name=$_SERVER["SCRIPT_NAME"];
echo $script_name."<br>";
$nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1);
echo $nav."<br>";
$nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//这句是去掉尾部的.html或.htm
echo $nav."<br>";
$vars = explode("/",$nav);
print_r($vars);
echo "<br>";
for($i=0;$i<Count($vars);$i+=2){
$_GET["$vars[$i]"]=$vars[$i+1];
}
return $_GET;
}
mod_rewrite();
$year=$_GET["year"];//结果为'2006'
echo $year."<br>";
$action=$_GET["action"];//结果为'_add'
echo $action;
?>
<?php
//伪静态方法四
//利用server变量 取得PATH_INFO信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分
if(@$path_info =$_SERVER["PATH_INFO"]){
//正则匹配一下参数
if(preg_match("//(d+),(d+),(d+).html/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]); //取得值 1
$sid =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
}else die("Path:Error!");
//相当于soft.php?gid=1&sid=100&softid=8630
}else die('Path:Nothing!');
?>
|
如果你有服务器权限我觉得还是使用apache%CE%B1%BE%B2%CC%AC/" target="_blank">apache伪静态
一、Apache配置:
进入/etc/httpd/conf/目录下,打开httpd.conf文件。
启用rewrite
# LoadModule rewrite_module modules/mod_rewrite.so 去除前面的 #
启用.htaccess
AllowOverride None 修改为: AllowOverride All
二、Rewrite写法
服务器有配置文件不可能由我们来改,所以大多情况下要在网站的根目录下建一个.htaccess文件。
代码如下 |
复制代码 |
RewriteEngine on //启动rewrite引擎
RewriteRule ^/index([0-9]*).html$ /index.php?id=$1 //“([0-9]*)” 代表范围 用(.*)代表所有,下同。
RewriteRule ^/index([0-9]*)/$ /index.php?id=$1 [R] //虚拟目录 |
三、mod_rewrite 规则修正符
1) R 强制外部重定向
2) F 禁用URL,返回403HTTP状态码。
3) G 强制URL为GONE,返回410HTTP状态码。
4) P 强制使用代理转发。
5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N 重新从第一条规则开始运行重写过程。
7) C 与下一条规则关联
如果规则匹配则正常处理,以下修正符无效
8) T=MIME-type(force MIME type) 强制MIME类型
9) NS 只用于不是内部子请求
10) NC 不区分大小写
11) QSA 追加请求字符串
12) NE 不在输出转义特殊字符 %3d$1 等价于 =$1
这里给大家介绍一个移动设备检查类了,从google找到的mobile-detect类,这个类可以ct判断android、ios、Tablet平板,浏览器,等比较完美完善的判断ipad,iPhone,win8等
使用方法
代码如下 |
复制代码 |
<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
// Any mobile device.
}
if($detect->isTablet()){
// Any tablet device.
}
Check for a specific platform:
if($detect->isiOS()){
// Code to run for the Apple's iOS platform.
}
if($detect->isAndroidOS()){
// Code to run for the Google's Android platform.
}
|
官方实例
代码如下 |
复制代码 |
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
$scriptVersion = $detect->getScriptVersion();
?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<title>Mobile Detect Local Demo</title>
<style type="text/css">
html { font-size: 100%; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
body { margin: 0; padding: 0 1em; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1em; color: #333333; background-color: #ffffff; max-width:320px; }
body, td { font-size: 1em; }
table th { text-align:left; }
a { color: #0088cc; text-decoration: underline; }
a:hover { color: #005580; text-decoration: underline; }
header h1 small { font-size:small; }
section { margin-bottom:2em; }
section h1 { font-size:100%; }
.infoText { font-size:85%; }
.response { color:red; }
.computer { background-color:blue; color:white; }
.tablet { background-color:yellow; color:black; }
.phone, .true { background-color:green; color:white; }
.sendDataButton { border-radius: 1em; -moz-border-radius: 1em; -webkit-border-radius: 1em; padding:0.5em 1em; cursor: pointer; }
.sendDataButton_yes {
color:white;
border: 1px solid #56A00E;
background: #74B042;
font-weight: bold;
color: #ffffff;
text-shadow: 0 1px 0 #335413;
background-image: -webkit-gradient(linear, left top, left bottom, from( #74B042 ), to( #56A00E )); /* Saf4+, Chrome */
background-image: -webkit-linear-gradient( #74B042 , #56A00E ); /* Chrome 10+, Saf5.1+ */
background-image: -moz-linear-gradient( #74B042 , #56A00E ); /* FF3.6 */
background-image: -ms-linear-gradient( #74B042 , #56A00E ); /* IE10 */
background-image: -o-linear-gradient( #74B042 , #56A00E ); /* Opera 11.10+ */
background-image: linear-gradient( #74B042 , #56A00E );
}
.sendDataButton_no {
color:white;
border: 1px solid #cd2c24;
background: red;
font-weight: bold;
color: #ffffff;
text-shadow: 0 1px 0 #444444;
background-image: -webkit-gradient(linear, left top, left bottom, from( #e13027 ), to( #b82720 )); /* Saf4+, Chrome */
background-image: -webkit-linear-gradient( #e13027 , #b82720 ); /* Chrome 10+, Saf5.1+ */
background-image: -moz-linear-gradient( #e13027 , #b82720 ); /* FF3.6 */
background-image: -ms-linear-gradient( #e13027 , #b82720 ); /* IE10 */
background-image: -o-linear-gradient( #e13027 , #b82720 ); /* Opera 11.10+ */
background-image: linear-gradient( #e13027 , #b82720 );
}
#feedbackForm fieldset { border:1px dotted #333; }
#feedbackForm label { font-weight:bold; font-size:85%; }
#feedbackForm textarea { width: 260px; }
</style>
</head>
<body>
<section>
<p>This is a <b><?php echo $deviceType; ?></b>. Your UA is <b class="<?php echo $deviceType; ?>"><?php echo htmlentities($_SERVER['HTTP_USER_AGENT']); ?></b></p>
</section>
<!-- copy to GitHub demo.php -->
<section>
<h1>Supported methods</h1>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<th colspan="2">Basic detection methods</th>
</tr>
<tr>
<td>isMobile()</td><td <?php $check = $detect->isMobile(); if($check): ?>class="true"<?php endif; ?>><?php var_dump($check); ?></td>
</tr>
<tr>
<td>isTablet()</td><td <?php $check = $detect->isTablet(); if($check): ?>class="true"<?php endif; ?>><?php var_dump($check); ?></td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="2">Custom detection methods</th>
</tr>
<?php foreach($detect->getRules() as $name => $regex):
$check = $detect->{'is'.$name}();
?>
<tr>
<td>is<?php echo $name; ?>()</td>
<td <?php if($check): ?>class="true"<?php endif; ?>><?php var_dump($check); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tbody>
<tr>
<th colspan="2">Experimental version() method</th>
</tr>
<?php
foreach($detect->getProperties() as $name => $match):
$check = $detect->version($name);
if($check!==false):
?>
<tr>
<td>version(<?php echo $name; ?>)</td>
<td><?php var_dump($check); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tbody>
<tr>
<th colspan="2">Other tests</th>
</tr>
<tr>
<td>isiphone()</td>
<td><?php echo var_dump($detect->isiphone()); ?></td>
</tr>
<tr>
<td>isIphone()</td>
<td><?php echo var_dump($detect->isIphone()); ?></td>
</tr>
<tr>
<td>istablet()</td>
<td><?php echo var_dump($detect->istablet()); ?></td>
</tr>
<tr>
<td>isIOS()</td>
<td><?php echo var_dump($detect->isIOS()); ?></td>
</tr>
<tr>
<td>isWhateverYouWant()</td>
<td class="randomcrap"><?php echo var_dump($detect->isWhateverYouWant()); ?></td>
</tr>
</tbody>
</table>
</section>
</body>
</html>
|
php-mobile-detect文件下载地焉: http://file.111cn.net/upload/2013/10/M.rar
以前做过一个招聘网站这样我们需要让别有采集不到我们客户的手机号码与邮箱地址了,所以我们会利用php实现从数据库读出来的手机号码与邮箱地址直接生成一张图片了,这样采集过去只能是图片并且无法识别了,下面我来给大家介绍两个实例。
PHP字符串处理-将手机号码生存图片
代码如下 |
复制代码 |
<?php
////电话号码转变成图片
//$str 要显示的字串,$rand是否启用扰码
function str_to_image($str,$w=130,$h=25,$rand=true)
{
//生成11位的数字图片
Header("Content-type:image/png"); //告诉浏览器,下面的数据是图片,而不要按文字显示
//定义图片宽高
$nwidth=$w;
$nheight=$h;
//srand((double)microtime()*1000000); //取得目前时间的百万分之一秒值,以执行时的百万分之一秒当乱数种子
$randval=$str; //11位数
$im=@imagecreate($nwidth,$nheight) or die("Can't initialize new GD image stream"); //建立图象
//图片色彩设置
$background_color=imagecolorallocate($im,255,255,255); //匹配颜色
$text_color=imagecolorallocate($im,23,14,91);
//绘制图片边框
imagefilledrectangle($im,0,0,$nwidth-1,$nheight-1,$background); //矩形区域着色
imagerectangle($im,0,0,$nwidth-1,$nheight-1,$background_color); //绘制矩形
imagestring($im,8,10,4,$randval,$text_color); //绘制横式字串
if($rand){
//加入干扰因素
for($i=0;$i<260;$i++)
{
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im,rand()%($nwidth-5),rand()%($nheight+5),$randcolor); //点
}
}
//imagestring($im,3,5,5,"A Simple Text String",$text_color);
//imageinterlace($im,1);
imagepng($im); //建立png图型
imagedestroy($im); //结束图型
}
$str = '13087263453';
echo str_to_image($str,$w=130,$h=25,$rand=true)
|
例2
代码如下 |
复制代码 |
<?php //前面不要有空行
$id=$_GET[id];
include("admin/config.php");
$sql="select * from user where id=$id";
$data=mysql_fetch_array(mysql_query($sql));
$p=SBC_DBC($data[Phone],1);
function get_str($str,$strlen=16) {
$str=stripslashes($str);
for($i=0;$i<$strlen;$i++)
if(ord(substr($str,$i,1))>0xa0) $j++;
if($j%2!=0) $strlen++;
$tmp_str=substr($str,0,$strlen);
return $tmp_str;
}
if($p<>''){
//生成5位的数字图片
Header("Content-type:image/png"); //告诉浏览器,下面的数据是图片,而不要按文字显示
//定义图片宽高
$nwidth=120;
$nheight=25;
$im=@imagecreate($nwidth,$nheight) or die("Can't initialize new GD image stream"); //建立图象
//图片色彩设置
$background_color=imagecolorallocate($im,255,255,255); //匹配颜色
$text_color=imagecolorallocate($im,23,14,91);
//绘制图片边框
imagefilledrectangle($im,0,0,$nwidth-1,$nheight-1,$background); //矩形区域着色
imagerectangle($im,0,0,$nwidth-1,$nheight-1,$background_color); //绘制矩形
//srand((double)microtime()*1000000); //取得目前时间的百万分之一秒值,以执行时的百万分之一秒当乱数种子
//$randval=rand();
$randval=$p; //5位数
imagestring($im,8,10,2,$randval,$text_color); //绘制横式字串
//加入干扰因素
//for($i=0;$i<478;$i++)
//{
//$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
//imagesetpixel($im,rand()%100,rand()%30,$randcolor); //点
//}
//imagestring($im,3,5,5,"A Simple Text String",$text_color);
//imageinterlace($im,1);
imagepng($im); //建立png图型
imagedestroy($im); //结束图型
}else{
echo "<font size=2>商家未输入电话号码</font>";
}
?>
|
如果我们是一维数据去重复项就不要说了有直接的函数了,但二维数据php未提供函数了,下面小编来给大家推荐两个不错的二维数组去重复项自定义函数吧。
例1
代码如下 |
复制代码 |
<?php
function unique_array_2d($array2D,$stkeep=false,$ndformat=true)
{
// 判断是否保留一级数组键 (一级数组键可以为非数字)
if($stkeep) $stArr = array_keys($array2D);
// 判断是否保留二级数组键 (所有二级数组键必须相同)
if($ndformat) $ndArr = array_keys(end($array2D));
//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
foreach ($array2D as $v){
$v = join(",",$v);
$temp[] = $v;
}
//去掉重复的字符串,也就是重复的一维数组
$temp = array_unique($temp);
//再将拆开的数组重新组装
foreach ($temp as $k => $v)
{
if($stkeep) $k = $stArr[$k];
if($ndformat)
{
$tempArr = explode(",",$v);
foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
}
else $output[$k] = explode(",",$v);
}
return $output;
}
$array2D = array(
'first'=>array('title'=>'1111','date'=>'2222'),
'second'=>array('title'=>'1111','date'=>'2222'),
'third'=>array('title'=>'2222','date'=>'3333')
);
echo "<pre>";
print_r(unique_array_2d($array2D,true));
/**
* 将二维数组通过指定的 key 去重
*
* @param array $arr 要去重的数组
* @param array $by 指定key去重,该参数不指定将返回 array()
* @return array
*/
function array_multunique($arr,$by = array()) {
$temp = array();
foreach($arr as $key => $val) {
foreach($by as $v) {
$temp[$key] .= isset($val[$v]) ? $val[$v] : '';
}
}
return array_intersect_key($arr,array_unique($temp));
}
/*$aa = array (
array ('id' => 123, 'name' => '张三' ),
array ('id' => 123, 'name' => '李四' ),
array ('id' => 124, 'name' => '王五' ),
array ('id' => 125, 'name' => '赵六' ),
array ('id' => 126, 'name' => '赵六' )
);
$key = 'id';
array_multunique ($aa, array('id')); */
function array_remove_key($array,$keys){
if (!is_array($array) || !is_array($keys)){
return false;
}
foreach($array as $t){
foreach($keys as $k){
unset($t[$k]);
}
$doc[]=$t;
}
return $doc;
}
/*$array = array(
'0' => array('a' => 'aaaaa', 'b' => 'bbbbb', 'c' => array('d' => 'ddddd', 'e' => 'eeeee')),
'1' => array('a' => 'aaaaa', 'b' => 'bbbbb', 'c' => array('d' => 'ddddd', 'e' => 'eeeee'))
);
print_r( array_remove_key($array,array('c')));*/
function array_remove_key_val(&$a,$b,$c){
foreach ($a as $key=>$value){
if ( isset($value[$b]) && ($value[$b]==$c) ){
unset($a[$key]);
}
}
}
/*$a=array(
array('id'=>1,'num'=>10,'type'=>'news'),
array('id'=>2,'num'=>100,'type'=>'pic')
);
print_r( array_remove_key_val($a,"id","1") );*/
|
例2
代码如下 |
复制代码 |
/二维数组去掉重复值
function array_unique_fb($array2D){
foreach ($array2D as $v){
$v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
$temp[] = $v;
}
$temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
foreach ($temp as $k => $v){
$temp[$k] = explode(",",$v); //再将拆开的数组重新组装
}
return $temp;
}
//二维数组去掉重复值 并保留键值
function array_unique_fb($array2D){
foreach ($array2D as $k=>$v){
$v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
$temp[$k] = $v;
}
$temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
foreach ($temp as $k => $v){
$array=explode(",",$v); //再将拆开的数组重新组装
$temp2[$k]["id"] =$array[0];
$temp2[$k]["litpic"] =$array[1];
$temp2[$k]["title"] =$array[2];
$temp2[$k]["address"] =$array[3];
$temp2[$k]["starttime"] =$array[4];
$temp2[$k]["endtime"] =$array[5];
$temp2[$k]["classid"] =$array[6];
$temp2[$k]["ename"] =$array[7];
}
return $temp2;
}
|