1

When a print_r() an array $stats, I get the following:

Array ( [0] => Array ( [like] => 71 [dislike] => 372 [total] => 443 [like_s] => 78 [dislike_s] => 291 [total_s] => 369 [final] => 11 ))

I want to get the [dislike_s] value and put it into a variable.

I have attempted this:

$statss = $stats['dislike_s'];

But it did not work. I have also tried $statss = $stats['dislike_s'][0]; without result.

What am I doing wrong?

1
  • 1
    $total_revision[0]['dislike_s'], the first array index is 0. Commented Aug 30, 2014 at 16:51

2 Answers 2

1

You are missing a level of your array

Array ( [0] => Array ( [like] => 71
       //^ this level

Also you are using the wrong variable, as you said your array is stored in $stats so

$total_revision = $stats[0]['dislike_s'];
Sign up to request clarification or add additional context in comments.

1 Comment

That was a complete typo, sorry, I was missing the [0] part. thank you, answer is correct.
0

$stats is a two dimensional Array, i.e. it is an array of arrays. You can see this from the output of print_r. You have something that looks like `Array ( [0]=>Array(...)). Therefore, when accessing an element of the inside array, you can think of it like this:

$inner_array=$stats[0];
$total_revisions=$inner_array['dislike_s'];

Php gives you a shorthand for combining these steps(pretty much all languages do) and it looks like $total_revisions=$stats[0]['dislike_s'] but intuitively it's the same thing. You're saying "in the array $stats[0] give back the value of array element 'dislike_s'"

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.