Convert value/pair list (simple array) into JSON with properties using PHP -



Convert value/pair list (simple array) into JSON with properties using PHP -

i have simple value pair array :

array(4) ( [4] => (string) barcelona [3] => (string) cordoba [1] => (string) granada [2] => (string) jaen )

i need encode json respond ajax request in next format :

[{"pk": 4, "name": "barcelona"}, {"pk": 3, "name": "cordoba"}, {"pk": 1, "name": "granada"}, {"pk": 2, "name": "jaen"}]

if utilize :

json_encode($a)

i next :

{"4":"barcelona","3":"cordoba","1":"granada","2":"jaen","0":"select province"}

how php format simple array include properties in json?

just create new array:

$data = array(); foreach($array $key => $value) { $data[] = array('pk' => $key, 'name' => $value); } $json = json_encode($data);

php json

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -