Nicky James
Nicky James

Reputation: 29

better output formatting using json_encode

How to produce result like

"alpa":[{"a":"a"},{"b":"b"},{"c":"c"}]

so I can easily get them in JS.

My code currently is like this:

$arr = array();
$arr["a"] = $a;
$arr["b"] = $b;
$arr["c"] = $c;

echo json_encode($arr);

output

{"a":"a"},{"b":"b"},{"c":"c"}

Upvotes: 2

Views: 33

Answers (2)

Pramod
Pramod

Reputation: 2896

Try this

$arr = array();
$arr['alpa']["a"] = $a;
$arr['alpa']["b"] = $b;
$arr['alpa']["c"] = $c;
echo json_encode($arr);

Upvotes: 1

Developerium
Developerium

Reputation: 7265

array(
    'alpa' => array(
        'a' => 'a',
        'b' => 'b',
        'c' => 'c',
    )
);

Upvotes: 1

Related Questions