资讯

精准传达 • 有效沟通

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

php循环对象数据 php对象是什么类型的数据

php 中while(对象)什么意思

mysql_fetch_field()取出该表的字段的信息啊,取一个,mysql_fetch_field()内部对偏移量+1,然后取下个字段的信息,取到最后一个了,再运行时mysql_fetch_field就返回false拉,然后就跳出循环啊

成都创新互联公司主营鸡西梨树网站建设的网络公司,主营网站建设方案,重庆APP开发,鸡西梨树h5小程序开发搭建,鸡西梨树网站营销推广欢迎鸡西梨树等地区企业咨询

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

foreach($project as $item){

echo $item-sample_status; 

}

PHP数组里循环对象的问题

?php

session_start();

include ("CartItem.class.php");

$cart = array ();

$cart = $_SESSION['gouwu'];

$id = $_GET['id'];

$name = $_GET['name'];

$price = $_GET['price'];

if ($cart == null) {

$pin = new CartItem();

$pin-setId($id);

$pin-setName($name);

$pin-setPrice($price);

$cart = $pin;

$_SESSION['gouwu'] = $cart;

?

table

tr

td商品/tdtd数量/tdtd价钱/td

?php

for ($i = 0; $i count($cart); $i++) {

$cart[$i]-getId();

?

trtd?=$pin-getId() ?/tdtd?=$pin-getName() ?/tdtd?=$pin-getPrice() ?/td /tr

?php

}}

?

/tr

什么错误不贴出来,只能猜着改了

不能使用数组类型的对象__PHP_Incomplete_Class

很简单,$cart是数组,怎么下面来obj那?直接用数组循环出来试试 我太明白你写的代码的意思 有点乱

如何获取php foreach循环出来的其中一条数据

foreach

语法结构提供了遍历数组的简单方式。foreach

仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。有两种语法:

foreach

(array_expression

as

$value)

statement

foreach

(array_expression

as

$key

=

$value)

statement

第一种格式遍历给定的

array_expression

数组。每次循环中,当前单元的值被赋给

$value

并且数组内部的指针向前移一步(因此下一次循环中将会得到下一个单元)。

第二种格式做同样的事,只除了当前单元的键名也会在每次循环中被赋给变量

$key。

范例:

$a

=

array(

"one"

=

1,

"two"

=

2,

"three"

=

3,

"seventeen"

=

17);foreach

($a

as

$k

=

$v)

{

echo

"\$a[$k]

=

$v.\n";}

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中,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对象是什么类型的数据
当前链接:http://cdkjz.cn/article/hhepcj.html
多年建站经验

多一份参考,总有益处

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

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

大客户专线   成都:13518219792   座机:028-86922220