php遍历get数据 php怎么遍历数据库里的数据( 三 )


}
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 = https://www.04ip.com/post/$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;
/**
【php遍历get数据 php怎么遍历数据库里的数据】* @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数组遍历取值问题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 遍历结果集 为什么取不到值有几种可能,你可以通过以下方式一一排除:
1.$rx_pro=$db-get_results('select * from'.TB_PREFIX.'product wherecategoryId=10');这一行并没有从数据库中取出数据,你可以在下方输入:print_r($rx_pro);来看看到底有没有得到 。
2. 确定数据库中已经存在数据,如果没有数据 , 当然得不到值 。要添加一些再测试,或者是没有满足categoryId字段为10的行 。
3. 另外 , 你确定$db这个类中的get_results方法是取得多行数据的?会不会是只取一行?如果这样,得到的count就不是二维数组了,你好好检查一下,你可以print_r一下结果看看 。

推荐阅读