json - encode_json with PHP and foreach issue -


i have php code pull database entries, , put them multi-dimensional array. need exact format, , cannot figure out how loop foreach.

        $res    = $globals["database"]->result("select * test");      $json = array     (         "data" => array         (             "entry" => array             (                 array                         (                 "player"    => $res["id"],                 "reason"    => $res["reason"],                 "postedtimestamp" => $res["posted"],                 "postedlong" => $postedlong              )             )          )     ); 

i cannot figure out put foreach, want loop through , pull entries , create new 'entry' each entry in db finds.

why not:

$data = array("data"=>array("entry"=>array())); foreach($res $r){     $data['data']['entry'][] = array(             "player"    => $res["id"],             "reason"    => $res["reason"],             "postedtimestamp" => $res["posted"],             "postedlong" => $postedlong          ); } 

this should work :)

doing this, yout "entry" array have numeric keys, can have many structures inside it. got it?


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -