PHP: Are arrays passed to a function automatically Immutable? -


i have been driving myself crazy past half hour , have checked various sites try , find solution, , suggesting have tried.

i pass array function write data client, php complains key doesn't exist trying accessed. fine, default thought if try set value unspecified key, php generate key , add array:

if(!isset($data['payload']))    $data['payload'] = 'no data resource'; 

the reason can think not working array immutable , therefore cannot appended to.

private function _setresponse($data, $statuscode = 200) {     header("http/1.1 " . $statuscode . " " . $this->_getstatus($statuscode));      if(!isset($data['payload']))         $data['payload'] = 'no data resource';      // set response object     $response = array(                         'response' => $statuscode . " : " . $this->_getstatus($statuscode),                         'data' => $data['payload']                      );      return json_encode($response); } 

you need pass reference if want modify array without explicitly returning @ end of function. change function such:

private function _setresponse(&$data, $statuscode = 200) 

you can read more on passing reference in php manual.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -