views:

53

answers:

2

$jj_post is the array output debug via print_r. this variable is an array of object

Array
(
    [0] => stdClass Object
        (
            [ID] => 2571
        )

)

i access the object property, ID, via code like this :

$jj_post_id = $jj_post[0]; 
$jj_ID = $jj_post_id->ID;

so, is there a better way, cause this is the only thing i know and i feel the code little bit too long ?

+3  A: 
$jj_ID = $jj_post[0]->ID;
Galen
+1  A: 

Well if you are sure $jj_post is always going to be an array, and that it will always contain an stdClass object, then you should access like so:

$jj_ID = $jj_post[0]->ID;

But it is not always so sometimes. You may not always know the contents of a variable so you need to do some checking to verify that you access areas that are safe and available.

How long the code is shouldn't be an issue if it performs the job well.

In my opinion, you have two alternatives:

 $jj_ID = @$jj_post[0]->ID;

This ensures that the runtime errors are silently handled, and not thrown to the standard output.

Another way would be to check absolutely for presence of each type:

$jj_ID = "";
if(is_array($jj_post))
{
  $jj_post_id = $jj_post[0]; 
  if(!empty($jj_post_id))
  {
    $jj_ID = $jj_post_id->ID;
  }
}
Olaseni
thank you very much. I owe you one. you even give me code that i probably will need forever ;D
justjoe