PHP : Work with stdClass() for create a json -


my goal read complex sqlite database php , , create json represents database according rules

to decided use stdclass() class

problem : when call test() function block3 not added

test code (i tried represent situation @ best, of course here not use database , foreach)

$obj = new stdclass();  $obj->block1 = array();  $obj->block1[0]["prop1"] = "test"; $obj->block1[0]["prop2"] = "test"; $obj->block1[0]["prop3"] = "test"; $obj->block1[0]["prop4"] = "test";    $obj->block2 = array(); for($i=0;$i<3;$i++) {     $obj->block2[$i]["sxsxs"] = "test";     $obj->block2[$i]["98u98u"] = "test";     $obj->block2[$i]["jhjh"] = "test";     $obj->block2[$i]["oiuoiu"] = "test";      //this works , want in test function     //$obj->block2[$i]["block3"] = array();     test($obj->block2[$i]);  }   function test($c){     $c["block3"] = array(); }  echo json_encode($obj); 

this want , correct , not add of block3

{     "block1": [         {             "prop1": "test",             "prop2": "test",             "prop3": "test",             "prop4": "test"         }     ],     "block2": [         {             "sxsxs": "test",             "98u98u": "test",             "jhjh": "test",             "oiuoiu": "test"             "block3" : []         },         {             "sxsxs": "test",             "98u98u": "test",             "jhjh": "test",             "oiuoiu": "test"             "block3" : []         },         {             "sxsxs": "test",             "98u98u": "test",             "jhjh": "test",             "oiuoiu": "test"             "block3" : []         }     ] } 

i not use call class test, divide various processes


the variable send test function not object. it's array.

do var_dump on $c inside test function , get:

array(4) { ["sxsxs"]=> string(4) "test" ["98u98u"]=> string(4) "test" ["jhjh"]=> string(4) "test" ["oiuoiu"]=> string(4) "test" }

you can't add variables using -> operator array. perhaps tried accomplish:

$obj = new stdclass();  $obj->block1 = array();  $obj->block1[0]["prop1"] = "test"; $obj->block1[0]["prop2"] = "test"; $obj->block1[0]["prop3"] = "test"; $obj->block1[0]["prop4"] = "test";    $obj->block2 = array(); for($i=0;$i<3;$i++) {     $obj->block2[$i]["sxsxs"] = "test";     $obj->block2[$i]["98u98u"] = "test";     $obj->block2[$i]["jhjh"] = "test";     $obj->block2[$i]["oiuoiu"] = "test";      test($obj);  }   function test($c){     $c->block3 = array(); }   echo json_encode($obj); 

output:

{"block1":[{"prop1":"test","prop2":"test","prop3":"test","prop4":"test"}],"block2":[{"sxsxs":"test","98u98u":"test","jhjh":"test","oiuoiu":"test"},{"sxsxs":"test","98u98u":"test","jhjh":"test","oiuoiu":"test"},{"sxsxs":"test","98u98u":"test","jhjh":"test","oiuoiu":"test"}],"block3":[]}


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -