Aniket Singh Aniket Singh - 1 year ago 124
JSON Question

php json encode from while loop

I'm using typehead on my page, here is how i json encode response

$name = array();
while($fetch = mysqli_fetch_assoc($go)){
$name[] = $fetch;

echo json_encode($name);

the encoded result look like this

[{"name":"Aniket Singh","img":"image.jpg","about":"Just testing"}]

now the typehead version I'm using requires json to be in this format

"value": "Aniket",
"name": "ANiket",
"description": "About me",
"icon": "thumbs-o-up",
"color": "success",
"tokens": [

how can i achieve this

Answer Source

I don't know where all your data is supposed to come from, but if you put the following in your while loop, it will give you the structure you're trying for:


  "value" => $fetch['name'],
  "name" => $fetch['name'],
  "description" => $fetch['about'],
  "icon" => $fetch['img'],
  "color" => "success",
  "tokens" => array( strtolower( $fetch['name'] ) )

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download