假設我有一個用戶定義類的對象數組。想知道如何在PHP中提取數組的元素。PHP Array提取對象
// class definition
class User
{
public $fname;
public $lname;
}
// array of objects of the class defined above
$objUser1 = new User():
$objUser2 = new User():
$objUser3 = new User():
$objUser4 = new User():
$alUser = array();
$alUser[] = $objUser1;
$alUser[] = $objUser2;
$alUser[] = $objUser3;
$alUser[] = $objUser4;
// trying to iterate and extract values using typcasting - this does not work, what is the alternative.
foreach($alUser as $user)
{
$obj = (User) $user; // gives error - unexpected $user;
}
這就是我如何使用java做的,同時提取了Java的ArrayList對象,因此認爲PHP的方法可能是相似的。任何人都可以解釋它。