當前位置:首頁 » 編程語言 » 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