Kruti Aparnathi Kruti Aparnathi - 4 months ago 26
JSON Question

How to create json object with php

How to create json object like this ? I want to make array and pass it in autocomplete function with json_encode

[
{label:"Display Name for result 1", value:1},
{label:"Display Name for result 2", value:2},
{label:"Display Name for result 3", value:3}
]

Answer

PHP doesn't use json itself. Just make a regular php array and push it through the json_encode function.

$stuff = array(
  array( 'label' => 'name 1', 'value' => 1 ),
  array( 'label' => 'name 2', 'value' => 2 ),
  array( 'label' => 'name 3', 'value' => 3 ),
);
echo json_encode( $stuff );