php對象循環
⑴ 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對象數組遍歷後獲取對象中的數據
foreach($projectas$item){
echo$item->sample_status;
}
⑶ php foreach遍歷對象
foreach只能遍歷數組,而不是對象!
⑷ 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>商品</td><td>數量</td><td>價錢</td>
<?php
for ($i = 0; $i < count($cart); $i++) {
$cart[$i]->getId();
?>
<tr><td><?=$pin->getId() ?></td><td><?=$pin->getName() ?></td><td><?=$pin->getPrice() ?></td> </tr>
<?php
}}
?>
</tr>
什麼錯誤不貼出來,只能猜著改了
不能使用數組類型的對象__PHP_Incomplete_Class
很簡單,$cart是數組,怎麼下面來obj那?直接用數組循環出來試試 我太明白你寫的代碼的意思 有點亂
⑸ 在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 對象在foreach中存入數組的key值後面的一個會被前面的覆蓋是怎麼回事
.... . . . .. 你那個存的始終都是一個變數 ,,,
foreach (a){
$box->goods_name[] = $xx;
}
你得這樣存才行
⑺ 關於PHP中對象迭代的問題
var1 => value 1
var2 => value 2
var3 => value 3
MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var
⑻ 2.php如何列印一個對象或數組到桌面上
php列印一個對象或數組到到桌面上:
1、列印數組使用print_r ( $array/$var )
print 是列印的意思,而r則取自Array的單詞,那麼該函數的功能就是列印數組內容,它既可以列印數組內容,也可以列印普通的變數。
print_r ($_REQUEST) ;
print_r ($_GET) ; /* 列印使用GET方法傳遞的表單內容*/
print_r($_POST) ; /* 列印使用表單POST方法傳遞過的數組內容*/
2、列印對象使用var_mp ($object/$array/$var)
var 代表變數(Variable),變數包括對象、數組以及標量變數,mp有倒出之意,加在一塊,就是將變數或對象的內容全部輸出出來。
var_mp($DB) ; /*列印$DB資料庫連接對象的內容*/
var_mp($fileHandle) ; /*列印文件句柄對象的內容*/
var_mp($Smarty) ; /*列印Smarty模板對象*/
⑼ php 如何在控制器中循環生成對象
<?php
classUserListController{
var$lv=array(2=>'2',3=>'3',4=>'4',5=>'5',6=>'6',7=>'7');
functionmain(){
$getController=$_GET['Controller'];
if(isset($getController)){
foreach($this->lvas$key=>$value){
$list=newUserList();
$array=$list->showUserListFun($this->lv[$key]);
$num=$list->showUserNumFun($this->lv[$key]);
unset($list);
}
require('../View/admin_user_list.php');//輸出模板
}
}
}
?>
要我加註釋么?能看懂吧~
⑽ 如何獲取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";}