导航:首页 > 编程语言 > php如何遍历对象

php如何遍历对象

发布时间:2023-03-09 11:44:57

php中如何用for循环遍历数组中的元素只是想用for循环哦

echo
get_all($arr);
function
get_all($arr){
$count
=
count($arr);
for($i=0;$i<$count;$i++){
if(is_array($arr[$i])){//判断是否为数组
get_all($arr[$i]);
}else{
echo
$arr[$i];
}
}
}
这种的通常都用递归迭代出来,仅供参考,希望能帮到你~

Ⅱ php foreach只能遍历数组么

foreach肯定可以遍历数组,但是有一些非数组的对象,有数组的特点也能通过foreach遍历出来

Ⅲ 在PHP中遍历对象用什么

其实网络一下就知道

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

[php] view plain
foreach( $array as $elem){
var_mp($elem);
}
[php] view plain
foreach($obj as $key=>$value){
echo "$key=>$value".PHP_EOL;
}

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

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

[php] view plain
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 plain
$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的博客:http://www.laruence.com/2008/11/20/630.html),那么可以很轻松地找到相应的方案。另外一方面,《设计模式-可复用面向对象软件设计的基础》中也提到:通过将对象的访问和遍历从对象中分离出来并放入一个迭代器对象中,迭代器模式可以实现以不同的方式对对象进行遍历。我们暂时不去深挖这句话的意思,只要知道,使用迭代器可以对对象进行遍历即可。

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

[php] view plain
abstractpublicmixedcurrent( void )

abstractpublicscalarkey( void )

abstractpublicvoidnext( void )

abstractpublicvoidrewind( void )

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

[php] view plain
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 plain
$it = new TestIterator;
$it->rewind();

while ($it->valid()){
$key = $it->key();
$value = $it->current();
echo "$key=>$value";
$it->next();
}
最后附上YII中ListIterator(顾名思义,实现对List的迭代操作的迭代器)的实现:

[php] view plain
<?php
/**
* CListIterator class file.
*
* @author Qiang Xue <[email protected]>
* @link http://www.yiiframework.com/
* @right Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/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 <[email protected]>
* @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中foreach($row as $key=>$val){}函数怎么理解,特别是$key=>$val

foreach 可以遍历数组与对象,它会把当前单元的键名也会在每次循环中被赋给变量 $key,值赋给变量$val,例如
$row=array('one'=>1,'two'=>2);
foreach($row as $key=>$val){
echo $key.'--'.$val;

}
第一次遍历的$key是one,$val是1;
第二次遍历的$key是two,$val是2;

Ⅳ php里的private值调用不到。类 遍历对象,如何取值pathName

取不到不是很正常吗,如果能取到那private的声明不就没意义了。
SplFileInfo应该提供getPathName之类的public方法来获取private $pathName的值。例如
public function getPathName() {
return $this->pathName;

}

阅读全文

与php如何遍历对象相关的资料

热点内容
安卓19怎么玩侠盗飞车 浏览:137
农业经济学pdf 浏览:721
有什么分享软件的app 浏览:41
电脑文件加密的文件怎么找出来 浏览:87
Java实现sha 浏览:525
购物车java代码 浏览:314
windows多核编程 浏览:607
股旁网选股指标公式源码 浏览:856
python运维管理工具 浏览:453
功率分配算法 浏览:489
钱豆豆app可以做什么 浏览:194
腾讯云服务器还需要数据库吗 浏览:767
比较器编程 浏览:738
苹果赏金赛和平精英是什么app 浏览:986
idea查找项目所在文件夹 浏览:163
程序员的电脑硬盘清理 浏览:686
安卓手机照片太多内存不够怎么办 浏览:959
施工命令号 浏览:59
javajpgtiff 浏览:872
忻奇医用静脉曲张压缩袜 浏览:896