Kruti Aparnathi
Kruti Aparnathi

Reputation: 175

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}
]

Upvotes: 0

Views: 7593

Answers (1)

Erik
Erik

Reputation: 3636

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 );

Upvotes: 2

Related Questions