0

I did a print_r on my array $total and it returned the following:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => stdClass Object ( 
[generated] => 6 [magnitude] => 3 [log_pk] => 14 [result] => 0.5000 ) )
Array ( ) Array ( )

I need to be able to print out log_pk from within the stdClass Object. I have tried print $total[0]->log_pk but that was unsuccessful. The error was Undefined offset: 0. Any help is appreciated. Thanks.

8
  • What's with the Array ( ) Array ( ) Array ( ) Array ( ) Arr... at the beginning? Commented Aug 10, 2013 at 2:35
  • It looks like that array is at index 4? Commented Aug 10, 2013 at 2:36
  • @DaveChen It looks like consecutive print_r on empty arrays. Commented Aug 10, 2013 at 2:38
  • This $total variable is the result of a MySQL query, each Array() I'm assuming represents a particular day as the query was based on the last 7 days. I also tried, $total[4][0]['log_pk] and there was no luck. Commented Aug 10, 2013 at 2:38
  • @PRPGFerret in which case he wouldn't get Undefined offset: 0 Commented Aug 10, 2013 at 2:39

4 Answers 4

1

So this is within a loop, you should check if the index 0 exists first.

if (isset($total[0])) echo $total[0]->log_pk
Sign up to request clarification or add additional context in comments.

Comments

1

Are you doing this within a loop? If so then it looks like the array is empty on most iterations. Try:

if (!empty($total)) print $total[0]->log_pk;

Comments

0

var_dump() displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

var_dump($total)

PHP: var_dump - Manual

1 Comment

I'm trying to get a particular value out of the $total array, not dump the whole lot?
0

it looks like your print_r is inside a loop.

while(true){
    $total = some_function();
    print_r($total);
    if($condition) break;
}
// Here - outside the loop, is not the right place for the print_r();

If you want you print outside the loop, you would change $total = some_function(); to $total[] = some_function(); and then you can do print_r($total[index])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.