json - PHP array to an Array of Objects -


how convert array array of objects php ?

input

[1, 2, 3] 

output

[ {id:1}, {id:2}, {id:3} ] 

if defined object before, guess i'd loop

     class previouslydefinedobject{        public $id;      }      $myarray = array(1,2,3);      $newarray = array();      foreach($myarray $id){        $obj = new previouslydefinedobject();        $obj->id = $id;        array_push($newarray, $obj);      }      print_r($newarray); 

that way, $newarray contains every object in array


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -