当前位置:首页 » 编程语言 » php对象的遍历

php对象的遍历

发布时间: 2022-07-06 14:16:04

php 怎么样遍历

第一、foreach()

foreach()是一个用来遍历数组中数据的最简单有效的方法。

<?php
$urls= array('aaa','bbb','ccc','ddd');
foreach ($urls as $url){
echo "This Site url is $url! <br />";
}
?>
显示结果:
This Site url is aaa

This Site url is bbb
This Site url is ccc
This Site url is ddd

第二、while() 和 list(),each()配合使用。

<?php

$urls= array('aaa','bbb','ccc','ddd');
while(list($key,$val)= each($urls)) {
echo "This Site url is $val.<br />";
}
?>

显示结果:

?

This Site url is aaa
This Site url is bbb
This Site url is ccc
This Site url is ddd
第三、for()运用for遍历数组

<?php
$urls= array('aaa','bbb','ccc','ddd');
for ($i= 0;$i< count($urls); $i++){
$str= $urls[$i];
echo "This Site url is $str.<br />";
}
?>
显示结果:

This Site url is aaa

This Site url is bbb
This Site url is ccc
This Site url is ddd

这几种遍历数组的方法哪个更快捷些呢,下面做个简单的测试就明白了
=========== 下面来测试三种遍历数组的速度 ===========
一般情况下,遍历一个数组有三种方法,for、while、foreach。其中最简单方便的是foreach。下面先让我们来测试一下共同遍历一个有50000个下标的一维数组所耗的时间。

<?php
$arr= array();
for($i= 0; $i< 50000; $i++){
$arr[]= $i*rand(1000,9999);
}
function GetRunTime()
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec+(float)$sec);
}
######################################
$time_start= GetRunTime();
for($i= 0; $i< count($arr); $i++){
$str= $arr[$i];
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of for:'.round($time_used, 7).'(s)<br /><br />';
unset($str, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
while(list($key, $val)= each($arr)){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of while:'.round($time_used, 7).'(s)<br /><br />';
unset($str, $key, $val, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
foreach($arr as$key=> $val){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of foreach:'.round($time_used, 7).'(s)<br /><br />';
?>

测试结果:

Used time of for:0.0228429(s)
Used time of while:0.0544658(s)
Used time of foreach:0.0085628(s)

结果表明,对于遍历同样一个数组,foreach速度最快,最慢的则是while。从原理上来看,foreach是对数组副本进行操作(通过拷贝数组),而while则通过移动数组内部指标进行操作,一般逻辑下认为,while应该比foreach快(因为foreach在开始执行的时候首先把数组复制进去,而while直接移动内部指标。),但结果刚刚相反。原因应该是,foreach是PHP内部实现,而while是通用的循环结构。所以,在通常应用中foreach简单,而且效率高。在PHP5下,foreach还可以遍历类的属性。
希望能够喜欢。

② php foreach遍历对象

foreach只能遍历数组,而不是对象!

③ php怎样遍历查看对象中的方法

<?php
classa{
public$a=1;
publicfunctionaaa(){
echo"a";
}
}
$a=newa();
print_r(get_class_methods($a));
?>

④ PHP 数组遍历方法大全(foreach,list,each)

在PHP中数组分为两类:
数字索引数组和关联数组。
其中数字索引数组和C语言中的数组一样,下标是为0,1,2…
而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。
下面介绍PHP中遍历关联数组的三种方法:
方法1:foreach
复制代码
代码如下:
<?php
$sports
=
array(
'football'
=>
'good',
'swimming'
=>
'very
well',
'running'
=>
'not
good');
foreach
($sports
as
$key
=>
$value)
{
echo
$key.":
".$value."<br
/>";
?>
输出结果:
football:
good
swimming:
very
well
running:
not
good
方法2:each
复制代码
代码如下:
<?php
$sports
=
array(
'football'
=>
'good',
'swimming'
=>
'very
well',
'running'
=>
'not
good');
while
($elem
=
each($sports))
{
echo
$elem['key'].":
".$elem['value']."<br
/>";
?>
方法3:list
&
each
复制代码
代码如下:
<?php
$sports
=
array(
'football'
=>
'good',
'swimming'
=>
'very
well',
'running'
=>
'not
good');
while
(list($key,
$value)
=
each($sports))
{
echo
$key.":
".$value."<br
/>";
?>

⑤ php数组遍历类与用法示例

本文实例讲述了php数组遍历类与用法。分享给大家供大家参考,具体如下:
<?php
class
scanArray{
public
$arr;
public
$where;
private
$str;
public
function
scan($arr,$where="array"){
$this->arr
=
$arr;
$this->where
=
$where;
foreach($this->arr
as
$k=>$v){
if(is_array($v)){
$this->where
=
($this->where)."[{$k}]";
$this->scan($v,$this->where);
}else{
$this->str
.=
$this->where."[{$k}]=".$v.'<br
/>';
}
}
return
$this->str;
}
function
__destruct(){
unset($this->arr);
unset($this->where);
}
}
$a
=
array('g'=>"a",'vv'=>array("b"=>"b","l"=>"c","xx"=>array("e","g")));
$ah
=
new
scanArray();
$b
=
$ah->scan($a);
echo
$b;
运行结果:
array[g]=a
array[vv][b]=b
array[vv][l]=c
array[vv][xx][0]=e
array[vv][xx][1]=g
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》及《PHP常用遍历算法与技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:PHP遍历数组的方法汇总PHP
数组遍历方法大全(foreach,list,each)PHP
数组遍历foreach语法结构及实例PHP中多维数组的foreach遍历示例php实现遍历多维数组的方法PHP中使用foreach()遍历二维数组的简单实例PHP遍历数组的三种方法及效率对比分析PHP实现的操作数组类库定义与用法示例PHP数组操作类实例PHP数组生成XML格式数据的封装类实例

⑥ PHP遍历,在线等

<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
以下代码,直接运行即可

<?php

$a
='{"libextractor-strategy.so":"
sea-cache/extractor/plugins/bin/libextractor-strategy.so","libextractor-feature.so":"version
1.0.181.0","libextractor-strategy.so":"version
1.0.207.1","libextractor-feature.so":"
sea-cache/extractor/plugins/bin/libextractor-feature.so"}';
$a=str_replace('{','',$a);
$a=str_replace('}','',$a);
$a=str_replace('"','',$a);
$b=explode(',',$a);
$last=array();
foreach($bas$v){
$tmp=explode(':',$v);
$last[]=array($tmp['0']=>$tmp['1']);
unset($tmp);
}
print_r($last);

?>

⑦ 在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中遍历stdclass object的如何实现代码

用get_object_vars()函数转换成数组。也可以声明一下这个变量类型 $test = (array)$test;,效果是一样的。前者需要解析处理。后者就没有那么麻烦处理了。

⑨ 请指点一下如何遍历这个PHP对象

foreach遍历

你可以print_r看看

⑩ php对象数组遍历后获取对象中的数据

foreach($projectas$item){
echo$item->sample_status;
}

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:433
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:744
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:147
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:240
java驼峰 发布:2025-02-02 09:13:26 浏览:652
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726