0

I have an array stored in a variable--at least I think it is an array--and want to get a value from it.

This is what the $myvar logs as and why I think it is an array:

Array
(
    [numberofhits] => 5
)

How can I get the value 5 into a variable?

Have tried some likely possibilities such as

 $numhits = $myvar->numberofhits;

But $numhits is empty.

Thanks for any suggestions.

6
  • 1
    $numhits = $myvar['numberofhits']; Commented May 11, 2017 at 20:11
  • You already asked this: stackoverflow.com/questions/43922830/… Commented May 11, 2017 at 20:12
  • 1
    As you said, its an array, not an object, so you have to access trough $var['elem'] insteadof $var->elem Commented May 11, 2017 at 20:13
  • Optionally, you can cast the array to an object by doing $myvar = (object)$myvar; but that is overkill for what you're trying to do... Commented May 11, 2017 at 20:14
  • 1
    They're essentially the same. After you do json_decode() it's just a PHP object or array, and you use the same syntax to access the contents. Commented May 11, 2017 at 20:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.