php - Build array of keys and values? -


im trying build json array in php structure:

[{"id":"name last name",   "id":"name last name",   "id":"name last name" }] 

where id key different number, not id string

im trying this:

for ($i = 0; $i < count($array); $i++){     //$namesarray[] = array($array[$i]["id"] =>$array[$i]["name"].     //               " ".$array[$i]["last"]." ".$array[$i]["name"]);      $namesarray[] = array_fill_keys(         $array[$i]["id"],         $array[$i]["name"]." ".             $array[$i]["last"]." ".             $array[$i]["name"]     ); }  echo json_encode($namesarray); 

with commented lines this:

[{"id":"name last name"},  {"id":"name last name"} ] 

but dont want that, want keys , values in single array.

thanks.

here how can it:

// sample data $array = array(     array("id" => 1, "name" => "james", "last" => "last"),     array("id" => 2, "name" => "micheal", "last" => "jackson"), );  // create empty object (associative array) $obj = (object) array();  // add key/value pairs object foreach ($array $row) {     $obj->$row["id"] = $row["name"] . " " . $row["last"]; }  // wrap object in single-element array $result = array($obj);  // output json string echo json_encode($result, json_pretty_print); 

output:

[     {         "1": "james last",         "2": "micheal jackson"     } ] 

Comments