本文实例讲述了PHP实现根据数组的值进行分组的方法。分享给大家供大家参考,具体如下:
PHP根据数组的值分组,php array中没有自带这个函数但是很常用,今天写了出来记录一下。
代码:
$_array=array(
array(1,11,-05-18'),
array(2,11,-05-18'),
array(3,22,-05-18'),
array(4,22,-05-18'),
array(5,33,-05-19'),
array(6,33,-05-19'),
array(7,44,-05-19'),
array(8,44,-05-19'),
array(9,55,-05-20'),
array(10,55,-05-20'),
);
var_dump(array_val_chunk($_array));
functionarray_val_chunk($array){
$result=array();
foreach($arrayas$key=>$value) {
$result[$value[1].$value[2]][] =$value;
}
$ret=array();
//这里把简直转成了数字的,方便同意处理
foreach($resultas$key=>$value) {
array_push($ret,$value);
}
return$ret;
}
运行结果如下:
array(5) {
[0]=>
array(2) {
[0]=>
array(3) {
[0]=>
int(1)
[1]=>
int(11)
[2]=>
string(10)"2016-05-18"
}
[1]=>
array(3) {
[0]=>
int(2)
[1]=>
int(11)
[2]=>
string(10)"2016-05-18"
}
}
[1]=>
array(2) {
[0]=>
array(3) {
[0]=>
int(3)
[1]=>
int(22)
[2]=>
string(10)"2016-05-18"
}
[1]=>
array(3) {
[0]=>
int(4)
[1]=>
int(22)
[2]=>
string(10)"2016-05-18"
}
}
[2]=>
array(2) {
[0]=>
array(3) {
[0]=>
int(5)
[1]=>
int(33)
[2]=>
string(10)"2016-05-19"
}
[1]=>
array(3) {
[0]=>
int(6)
[1]=>
int(33)
[2]=>
string(10)"2016-05-19"
}
}
[3]=>
array(2) {
[0]=>
array(3) {
[0]=>
int(7)
[1]=>
int(44)
[2]=>
string(10)"2016-05-19"
}
[1]=>
array(3) {
[0]=>
int(8)
[1]=>
int(44)
[2]=>
string(10)"2016-05-19"
}
}
[4]=>
array(2) {
[0]=>
array(3) {
[0]=>
int(9)
[1]=>
int(55)
[2]=>
string(10)"2016-05-20"
}
[1]=>
array(3) {
[0]=>
int(10)
[1]=>
int(55)
[2]=>
string(10)"2016-05-20"
}
}
}
PHP的使用在现在越来越受用户的重视,很多人都在钻研PHP。今天文章就给大家带来简单的实例PHP编程怎么获取音频文件时长,有什么获取方法,感兴趣的下面一起来看看。问题:
昨天在新增论坛功能的时候,移动端显示音频文件需要知道是多长的音频;
具体解决方案如下:
首先就是数据库中增加保存音频文件时长的字段;
然后我在网上找到了getID3这个类可以获取到音频文件的时长,将其引入到项目中去;
其次在移动端上传音频文件的接口中,调用该类,具体写法如下:
$getID3=newgetID3(); //实例化类
$ThisFileInfo=$getID3->analyze($path);//分析文件,$path为音频文件的地址
$fileduration=$ThisFileInfo['playtime_seconds'];//这个获得的便是音频文件的时长
并将相应数据存入到数据库中;
然后在移动端需要数据列表的时候将音频时长同时返回过去就行
今天文章要给大家的介绍的是YII2框架中使用yii.js实现的post请求,一个简单实用的小实例,还不知道的赶紧来看看。yii2提供了很多帮助类,比如Html、Url、Json等,可以很方便的实现一些功能,下面简单说下这个Html。用yii2写view时时经常会用到它,今天在改写一个页面时又用到了它。它比较好用的地方就在于,它不仅仅是生成一个简单的html标签,结合yii2自己的静态资源文件yii.js可以很方便的实现一个post请求。
yii2将这些功能都做好了封装,只要在合适的地方调用它的方法就可以了,可以说yii2是个可以开箱即用的框架,你可以用它很快的实现一个需要的功能:比如在页面中放置一个删除按钮,点击按钮发送post请求,弹出确认对话框。如果没有yii\helpers\Html类和yii.js,那么你需要写大量的js/jquery来实现这个功能。如果用yii2的话,下面的代码就可以实现:
// html代码
<?= Html::a(
'删除',
[
'delete',
'id' => $id,
],
[
'data' => [
'confirm' => '你确定要删除吗?',
'method' => 'post',
],
]
)
?>
// html代码
它会在页面中生成下面一段html代码:
删除
点击这个按钮会弹出对话框,确认删除后会发送post请求。那么这个post请求是如何发送的呢?到现在为止可是一段js代码都没写呢。
yii2框架隐藏了技术实现的细节,post请求的实现在yii.js中。在yii.js中,定义了window.yii对象,并初始化了window.yii对象的initModule方法:
window.yii = (function($) {
varpub = {
// 定义了处理事件的方法,比如下面这个:
confirm:function(message, ok, cancel) {
if(window.confirm(message)) {
!ok || ok();
}else{
!cancel || cancel();
}
},
handleAction:function($e, event) {
var$form = $e.attr('data-form') ? $('#'+ $e.attr('data-form')) : $e.closest('form'),
method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'),
// 其他省略
},
// 其他省略
};
// 初始化模块
initModule:function(module) {
if(module.isActive !== undefined && !module.isActive) {
return;
}
if($.isFunction(module.init)) {
module.init();
}
$.each(module,function() {
if($.isPlainObject(this)) {
pub.initModule(this);
}
});
},
// 初始化方法
init:function() {
initCsrfHandler();
initRedirectHandler();
initAssetFilters();
initDataMethods();
},
returnpub;
})(window.jQuery);
window.jQuery(function() {
window.yii.initModule(window.yii);
});
其中上面的initDataMethods()会调用pub.handleAction方法:
functioninitDataMethods() {
varhandler =function(event) {
var$this= $(this),
method = $this.data('method'),
message = $this.data('confirm'),
form = $this.data('form');
if(method === undefined && message === undefined && form === undefined) {
returntrue;
}
if(message !== undefined) {
$.proxy(pub.confirm,this)(message,function() {
pub.handleAction($this, event);
});
}else{
pub.handleAction($this, event);
}
event.stopImmediatePropagation();
returnfalse;
};
// handle data-confirm and data-method for clickable and changeable elements
$(document).on('click.yii', pub.clickableSelector, handler)
.on('change.yii', pub.changeableSelector, handler);
}
可以看到这个方法会获取上面生成的a标签的data属性值,然后交给handlerAction来处理。handlerAction通过动态生成一个form来处理各种请求,最后通过触发submit事件来提交。
// 其他省略
$form = $('
', {method: method, action: action});
vartarget = $e.attr('target');
if(target) {
$form.attr('target', target);
}
if(!/(get|post)/i.test(method)) {
$form.append($('', {name:'_method', value: method, type:'hidden'}));
method ='post'
$form.attr('method', method);
}
if(/post/i.test(method)) {
varcsrfParam = pub.getCsrfParam();
if(csrfParam) {
$form.append($('', {name: csrfParam, value: pub.getCsrfToken(), type:'hidden'}));
}
}
$form.hide().appendTo('body');
// 其他省略
PS:做项目用框架很方便,但是框架用的久了,就容易把基本的技术给忘掉了。还是要打好基础呀,这样不管用什么框架都不至于用得云里雾里的。
代码如下 | 复制代码 |
话不多说,请看代码:
代码如下 | 复制代码 |
//在注册时用户名不能是纯数字, 防止一个人的用户名和另一个人的手机号相同 publicfunctionLogin(){ if(IS_AJAX) { $username= I('param.username'); //var_dump($username); $password= I('param.password'); $user= M("cuser"); $res=$user->where("login_id='{$username}' OR phone='{$username}'")->find(); if(!empty($res) &&$res['password']==md5($password)) { $status=1;//用户名正确,密码正确。 }elseif(!empty($res) &&$res['password']!==md5($password)){ $status=2;//用户名正确,密码不正确。 }elseif(empty($res)){ $status=3;//用户名不存在。 } $this->ajaxReturn($status); exit(); } $mobile= parent::isMobile(); if($mobile=="true"){ $this->display(Wap_Login); }else{ $this->display(Login); } } |