PHP objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
csam0003
Forum Newbie
Posts: 2
Joined: Tue Jan 18, 2011 6:18 am

PHP objects

Post 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
Last edited by Benjamin on Tue Jan 18, 2011 6:34 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP objects

Post 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] != "")
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP objects

Post 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"]) != "")
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply