资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php遍历get数据 php接收get数据

PHP 怎么样遍历

第一、foreach()

创新互联公司是一家专业提供惠水企业网站建设,专注与成都网站制作、成都网站设计、外贸营销网站建设H5建站、小程序制作等业务。10年已为惠水众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

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如何遍历数组

1、在test.php文件内,使用header设置test.php执行的编码为utf8,避免输出中文的时候出现乱码。

2、在test.php文件内,创建一个测试的数组,例如,定义一个分类的数组,其对应的索引值分别为0,4,8。

3、在test.php文件内,使用array_values()方法将上一步的数据重新排序,并且从0开始,把重新排序的数组保存在$result变量中。

4、在test.php文件内,使用foreach方法遍历数组,其中$k为索引值,$v为索引值对应的数组值。

5、在test.php文件内,使用echo方法输出数组中的索引值和对应的数组值即可。

php数组遍历取值问题

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遍历数据库问题

首先你要说你用的是什么数据库。用最普通的mysql数据库来说,php自带了一些操作数据库的函数。

首先你将语句写入一个变量:

$Query = "select * from A_table";

然后用mysql_query这个函数执行这条语句,并将输出结果放在一个变量中:

$Result = mysql_query($Query);

这个$Result变量就是一个资源变量,包含了所有符合条件的结果。要将结果处理,需要用另一个函数

mysql_fetch_assoc:

while($Row = mysql_fetch_assoc($Result))

{

//这里$Row就是遍历了结果的每一行。假设有个字段叫A_field,你要把它输出

echo $Row["A_field"];

//其他操作类似。

}

在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遍历get数据 php接收get数据
分享链接:http://cdkjz.cn/article/dodhgch.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220