-1

I feel insane for asking this, but I just can't find an answer to this very simple question.

I have an object: print_r:

stdClass Object
(
    [formkey] => 1F9trPeu9DA4W0CjADN4a1fl3Jh682ZPF8remWB21RhI
    [draftResponse] => []
    [pageHistory] => 0
    [entry.358412101] => asdf
    [entry.898829644] => asdf
    [entry.2071756716] => asdf
    [entry.958924423] => asdf
)

var_dump:

object(stdClass)#1 (7) {
  ["formkey"]=>
  string(44) "1F9trPeu9DA4W0CjADN4a1fl3Jh682ZPF8remWB21RhI"
  ["draftResponse"]=>
  string(2) "[]"
  ["pageHistory"]=>
  string(1) "0"
  ["entry.358412101"]=>
  string(4) "asdf"
  ["entry.898829644"]=>
  string(4) "asdf"
  ["entry.2071756716"]=>
  string(4) "asdf"
  ["entry.958924423"]=>
  string(4) "asdf"
}

I want to return a single value. I this is what thought you were suppose to do:

return($objData['formkey']);

but this returns PHP Fatal error: Cannot use object of type stdClass as array. What am I doing wrong?

I post the data as a json-objec to the server and then it converts it like this:

$data = file_get_contents("php://input");
$objData = json_decode($data);
Himmators
  • 14,278
  • 36
  • 132
  • 223

1 Answers1

2

The [key] syntax is for accessing an entry in an array.

As you can see from the dump, you have an Object. Object properties are accessed with ->key.

return $objData->formkey;

If you want to see error messages check the log files, or enable reporting: How to get useful error messages in PHP?

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • If I console.log(data) i get an empty string when doing that? – Himmators Sep 04 '13 at 09:45
  • @Kristoffer Why would you `console.log` data in Javascript for a PHP problem? – deceze Sep 04 '13 at 09:46
  • because I post using ajax. – Himmators Sep 04 '13 at 09:48
  • 1
    @Kristoffer But between *PHP code, ???, problem, ???, `console.log`* are about three gazillion more steps which may each cause an error. You can't debug a PHP problem client-side in Javascript. Focus on the PHP code first. – deceze Sep 04 '13 at 09:50
  • I assume you have an empty response. But it's impossible to tell as you haven't posted any related code... – Karoly Horvath Sep 04 '13 at 09:50
  • I had a solution before where I posted using a regular form , the problems ocured when I tried to catch the post that I did with Ajax. – Himmators Sep 04 '13 at 09:52
  • Yes, e.g.: you could drive your app with test data, `$data = "{..json data..}";`, that would completely eliminate the need to do ajax calls. – Karoly Horvath Sep 04 '13 at 09:53