好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>CMS系统>Dedecms
<tfoot draggable='sEl'></tfoot>

php循环输出对象 PHP输出数组用什么函数

很多站长朋友们都不太清楚php循环输出对象,今天小编就来给大家整理php循环输出对象,希望对各位有所帮助,具体内容如下:

本文目录一览: 1、 PHP 对象循环的问题 2、 php 如何在控制器中循环生成对象? 3、 在PHP中遍历对象用什么? 4、 php关于输出对象的方法的问题 PHP 对象循环的问题

可以将循环输出的东西,赋值给一个数组,并将该数组返回;

class name{

function name(){

while($result=$conf->fetch_array($query){

$two_array[]=$result;

}

return $two_array;

}

}

这样就把要循环的所有数据 赋值给二为数组 $two_array; 并返回该数组的值.然后把在其他页引用该数组;

<?php

include_once("上面的类")

$name = new name;

$define_array_name = $name->name();

用$define_array_name[0][1]的方式显示到表格中;

?>

php 如何在控制器中循环生成对象?

<?php

class UserListController {

    var $lv = array(2=>'2',3=>'3',4=>'4',5=>'5',6=>'6',7=>'7');

    function main(){

        $getController = $_GET['Controller'];

        if(isset($getController)){  

            foreach($this->lv as $key=>$value){

                $list = new UserList();

                $array = $list->showUserListFun($this->lv[$key]);

                $num = $list->showUserNumFun($this->lv[$key]);

                unset($list);

            }

            require('View/admin_user_list.php');//输出模板

        }

    }

}

?>

要我加注释么?能看懂吧~

在PHP中遍历对象用什么?

其实百度一下就知道

我们知道,php中,foreach可以很方便地对可迭代结构(例如数组,再如对象)进行迭代操作:

[php] view plaincopy

foreach( $array as $elem){

var_dump($elem);

}

[php] view plaincopy

foreach($obj as $key=>$value){

echo "$key=>$value".PHP_EOL;

}

因而我们想:如果对于一个实例化对象,对其进行foreach操作,会发生什么事情呢?

首先我们定义的基础类为:

[php] view plaincopy

Class Test{

/* one public variable */

public $a;

public $b;

/* one private variable */

private $c;

public function __construct(){

$this->a = "public";

$this->b = "public";

$this->c = "private";

}

public function traverseInside(){

foreach($this as $key=>$value){

echo $key."=>".$value.EOL;

}

}

}

然后我们实例化该类,对其进行迭代,并与内部迭代的结果进行比较:

[php] view plaincopy

$test = new Test;

echo "<hr>";

echo "traverse outside:".EOL;

foreach( $test as $key=>$value ){

echo $key."=>".$value.EOL;

}

echo "<hr>";

echo "traverse inside:".EOL;

$test->traverseInside();

迭代的结果为:

可以看出:外部foreach循环的结果,只是将对象的公有属性(public)循环出来了,而对于私有属性(private),外部foreach是无法循环出来的。因而我们如果想要在外部通过foreach循环出类的所有的属性(公有的和私有的),仅仅依靠foreach是不行的,必须要对类进行“改造”。如何对类进行改造呢?如果你了解foreach的实现(参考laruence的博客:),那么可以很轻松地找到相应的方案。另外一方面,《设计模式-可复用面向对象软件设计的基础》中也提到:通过将对象的访问和遍历从对象中分离出来并放入一个迭代器对象中,迭代器模式可以实现以不同的方式对对象进行遍历。我们暂时不去深挖这句话的意思,只要知道,使用迭代器可以对对象进行遍历即可。

PHP手册<预定义接口>部分指出:要实现迭代器模式,需要在可迭代对象中实现如下接口:

[php] view plaincopy

abstractpublicmixedcurrent( void )

abstractpublicscalarkey( void )

abstractpublicvoidnext( void )

abstractpublicvoidrewind( void )

abstractpublicbooleanvalid( void )

有了这个。实现迭代器模式就很方便了,一个简单的实例如下:

[php] view plaincopy

class TestIterator implements Iterator {

private $point = 0;

private $data = array(

"one","two","three",

);

public function __construct() {

$this->point = 0;

}

function rewind() {

$this->point = 0;

}

function current() {

return $this->data[$this->point];

}

function key() {

return $this->point;

}

function next() {

++$this->point;

}

function valid() {

return isset($this->data[$this->point]);

}

}

$it = new TestIterator;

foreach($it as $key => $value) {

echo $key, $value;

echo "\n";

}

当然,使用了迭代器的对象可以以如下方式进行遍历:

[php] view plaincopy

$it = new TestIterator;

$it->rewind();

while ($it->valid()){

$key = $it->key();

$value = $it->current();

echo "$key=>$value";

$it->next();

}

最后附上YII中ListIterator(顾名思义,实现对List的迭代操作的迭代器)的实现:

[php] view plaincopy

<?php

/**

* CListIterator class file.

*

* @author Qiang Xue <qiang.xue@gmail.com>

* @link

* @copyright Copyright © 2008-2011 Yii Software LLC

* @license

*/

/**

* CListIterator implements an interator for {@link CList}.

*

* It allows CList to return a new iterator for traversing the items in the list.

*

* @author Qiang Xue <qiang.xue@gmail.com>

* @version $Id$

* @package system.collections

* @since 1.0

*/

class CListIterator implements Iterator

{

/**

* @var array the data to be iterated through

*/

private $_d;

/**

* @var integer index of the current item

*/

private $_i;

/**

* @var integer count of the data items

*/

private $_c;

/**

* Constructor.

* @param array $data the data to be iterated through

*/

public function __construct($data)

{

$this->_d=$data;

$this->_i=0;

$this->_c=count($this->_d);

}

/**

* Rewinds internal array pointer.

* This method is required by the interface Iterator.

*/

public function rewind()

{

$this->_i=0;

}

/**

* Returns the key of the current array item.

* This method is required by the interface Iterator.

* @return integer the key of the current array item

*/

public function key()

{

return $this->_i;

}

/**

* Returns the current array item.

* This method is required by the interface Iterator.

* @return mixed the current array item

*/

public function current()

{

return $this->_d[$this->_i];

}

/**

* Moves the internal pointer to the next array item.

* This method is required by the interface Iterator.

*/

public function next()

{

$this->_i++;

}

/**

* Returns whether there is an item at current position.

* This method is required by the interface Iterator.

* @return boolean

*/

public function valid()

{

return $this->_i<$this->_c;

}

}

php关于输出对象的方法的问题

看一下,这个是不是你要的东西。

<?php

class myclass {

    // constructor

    var $t1='tt1';

    public $t2='tt2';

    protected $t3;

    private $t4;

                    

    function myclass()

    {

        return(true);

    }

    // method 1

    function myfunc1()

    {

        return(true);

    }

    // method 2

    function myfunc2()

    {

        return(true);

    }

}

$methods = get_class_methods('myclass');

print_r($methods);

$vars = get_class_vars('myclass');

print_r($vars);

?>

输出结果:

Array ( [0] => myclass [1] => myfunc1 [2] => myfunc2 ) Array ( [t1] => tt1 [t2] => tt2 )

关于php循环输出对象的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。

查看更多关于php循环输出对象 PHP输出数组用什么函数的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did163633
更新时间:2023-02-11   阅读:51次

上一篇: php文章管理系统教程 php文章管理系统教程图片

下一篇:学习php怎么赚钱 学php能干嘛

相关资讯

最新资料更新

  • 1.dede5.7解决senddate比pubdate时间晚的问题
  • 2.详解DEDECMS 多行导航菜单栏的实现方法
  • 3.dedecms注册中文会员无法打开空间的解决方法
  • 4.dedecms的dedesql.class.php on line 489错误的解决方法
  • 5.dedecms友情链接中去掉织梦链投放修改方法
  • 6.Dedecms自定义模型解决会员无法投稿的方法
  • 7.Dedecms网站给图片alt属性自动调用标题的方法
  • 8.解析DedeCms中data目录下的sessions是什么文件
  • 9.织梦DEDECMS友情链接出现内页与首页都在首页显示解决方法
  • 10.最新关于织梦DEDECMS文章排序方式及调用方法
  • 11.详解织梦模板DEDECMS核心类TypeLink.class.php功能分析
  • 12.织梦Dedecms中万能标签loop不能输入URL的解决方法
  • 13.DedeCms调用分类信息到首页并和栏目整齐排序方法
  • 14.DEDE调用分类及分类下文章并限制标题字数及显示条数
  • 15.DedeCMS系统自定义字段图片调用问题的解决方法
  • 16.织梦dedecms系统后台安全提示去除方法
  • 17.DedeCms后台登录一片空白的解决方法
  • 18.织梦登陆后台卡死无法进入的解决方法
  • 19.dedecms首页导航菜单二级栏目调用标签实例
  • 20.dedecms中英文网站之中英文搜索结果实现方法

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网」
[ SiteMap ]