How to get the array content when value is object?
I have an array, which contains different data types. Those are int and object. The array is as follows.
Array (
[isHR] => 1
[1] => Array (
[0] => MyQuota Object (
[year:private] => 2011
[leaveId:private] => L00开发者_开发百科1
[employeeId:private] =>
[NoOfDays:private] => 7.00
[leaveName:private] => Casual Leave
[Message:private] =>
)
[1] => MyQuota Object (
[year:private] => 2011
[leaveId:private] => LTY002
[employeeId:private] =>
[NoOfDays:private] => 55.00
[leaveName:private] => Priviledged Leave
[Message:private] =>
)
)
[Length] => 8
)
Here I need to get and set array attribute called "NoOfDays:private". How can I get this?
Add getters and setters:
class MyQuota
{
private $year;
private $leaveId;
private $employeeId;
private $NoOfDays;
private $leaveMessage;
private $Message;
/* more code */
public function getNoOfDays()
{
return $this->NoOfDays;
}
public function setNoOfDays($noOfDays)
{
$this->NoOfDays = $noOfDays;
}
/* more getters and setters */
}
And, with your array, use it like this:
$days = $array[0][1]->getNoOfDays();
One of the most easiest and straightforward ways, please refer to this page. [1] http://php.net/manual/en/function.get-object-vars.php
You may not have an access to private properties of an object. either make them public or define getter and setter methods.
to access the public properties:
echo $arr[1][0]->NoOfDays
精彩评论