PHP对象获取函数参数thinkphp对象与数组的转换
发布时间:2015-11-21, 12:27:45 分类:PHP | 编辑 off 网址 | 辅助
正文 2980字数 178,842阅读
if($count){
import ( "@.ORG.Page" );
$listRows = !empty($cat['pagesize']) ? $cat['pagesize'] : C('PAGE_LISTROWS');
$page = new Page ( $count, $listRows );
$page->urlrule = geturl($cat,'',$this->Urlrule);
//dump($page);exit;
$xl_nowPage=$page->nowPage;//当前页
$xl_totalRows=$page->totalRows;//总共多少篇文章
$xl_listRows=$page->listRows;//每页多少篇
$this->assign('page_current',$xl_nowPage);
$this->assign('page_total',$xl_totalRows);
$this->assign('page_perpage',$xl_listRows);
$pages = $page->show();
$field = $this->module[$this->mod[$module]]['listfields'];
$field = $field ? $field : 'id,catid,userid,url,username,title,title_style,keywords,description,thumb,createtime,hits,content';
$list = $this->dao->field($field)->where($where)->order('createtime desc,id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
//dump($list);exit;
$this->assign('pages',$pages);
$this->assign('list',$list);
}
Run code
Cut to clipboard
php简单对象与数组的转换
function arrayToObject($e){
if( gettype($e)!='array' ) return;
foreach($e as $k=>$v){
if( gettype($v)=='array' || getType($v)=='object' )
$e[$k]=(object)arrayToObject($v);
}
return (object)$e;
}
function objectToArray($e){
$e=(array)$e;
foreach($e as $k=>$v){
if( gettype($v)=='resource' ) return;
if( gettype($v)=='object' || gettype($v)=='array' )
$e[$k]=(array)objectToArray($v);
}
return $e;
}
Run code
Cut to clipboard
对象和数组的相互转化在开发中也是很常见,一般不是多维的情况下直接(array)和(object)就可搞定了,多维的话,遍历下也就可以了:
<?php
/**
* 对象和数组的相互转化
* @link http://www.phpddt.com PHP分享平台
*/
class Test{
public $a;
public $b;
public function __construct($a) {
$this->a = $a;
}
}
//对象转数组,使用get_object_vars返回对象属性组成的数组
function objectToArray($obj){
$arr = is_object($obj) ? get_object_vars($obj) : $obj;
if(is_array($arr)){
return array_map(__FUNCTION__, $arr);
}else{
return $arr;
}
}
//数组转对象
function arrayToObject($arr){
if(is_array($arr)){
return (object) array_map(__FUNCTION__, $arr);
}else{
return $arr;
}
}
$test = new Test('test1');
$test->b = new Test('test2');
print_r($test);
$array = objectToArray($test);
print_r($array);
$object = arrayToObject($array);
print_r($object);
Run code
Cut to clipboard
array(1) {
[0]=> object(stdClass)#3059 (13)
{ ["id"]=> string(2) "57"
["average_review"]=> string(3) "0.0"
["last_voted"]=> string(19) "2014-03-26 11:53:56"
["rules"]=> string(1) "A"
["moderate"]=> string(1) "N"
} }
echo $b[0]->id
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
暂无评论 »