Page 1 of 1

PHP objects

Posted: Tue Jan 18, 2011 6:23 am
by csam0003
Hi all,

I have this object

Code: Select all

$my_data -> monthly_performance[$year]["M$m"][] = $x
I now wish to access this object directly.

Why cant I do something like this?

Code: Select all

if ($my_data -> monthly_performance[2009]["M1"][] != "")
{
do something
} 
thanks

Re: PHP objects

Posted: Tue Jan 18, 2011 6:34 am
by Benjamin
Because [] is the same as array_push(). You need to know the key that was assigned to the value in order to access it. e.g.

Code: Select all

if ($my_data -> monthly_performance[2009]["M1"][0] != "")

Re: PHP objects

Posted: Tue Jan 18, 2011 10:38 am
by AbraCadaver
Or if you are sure that was the last element added you can do this (probably not what you want):

Code: Select all

if ( end($my_data -> monthly_performance[2009]["M1"]) != "")