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
Post a Comment