json - getting a value of a known key in php failing -


i know it's syntax, can't find problem.

i use loop turn json keys variables this:

sent json: [{\"name\":\"dolly\",\"page\":\"a4\"}]

$object = json_decode(stripslashes($_post['mydata']));  foreach ($object[0] $key => $value) {        $$key = preg_replace('/--+/',' ',$value);    } 

so now, eg, have $page = "a4". works fine.

now, rather looping through that, want access 'page' key (that know going there every time), , disregard else.

i thought it, falls on "cannot use object of type stdclass array":

$object = json_decode(stripslashes($_post['mydata']));  $page = $object[0]['page']; 

this doesn't error out, returns nothing:

$object = json_decode($_post['mydata']);  $p = $object[0]->page; 

as does

$p = $object->page; 

what screwing here?

thanks taking look.

you need combine approaches ;-)

$object = json_decode(stripslashes($_post['mydata'])); // note stripslashes() here! $p = $object[0]->page; 

as object encoded array, need first element , object property did in second snippet of code. forgot apply stripslashes() json_decode() failed.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -