user6579134
user6579134

Reputation: 839

php - echo to return results with curly and square brackets

i have a script that returns the json results in this

Figure 1

["No=>0001","Name=>DONALD FRIMP","Address=>B BLOCK 2"]

but i want it to return like this

Figure 2

[{"No":"0001","Address":"B BLOCK 2"}]

this is my php

$data=array('No=>'.$customer->No,Address=>'.$customer->Address);
echo json_encode($data);

Any help on how to get json to return like figure 2.

Upvotes: 0

Views: 393

Answers (3)

imskm
imskm

Reputation: 805

try this

$data=array('No'=>$customer->No,'Address'=>$customer->Address);
echo json_encode(array($data));

Upvotes: 1

rickdenhaan
rickdenhaan

Reputation: 11328

Use a proper array:

$data=array('No' => $customer->No, 'Address' => $customer->Address);
echo json_encode($data);

That will output a JSON object. If you want it wrapped in a list in JSON, wrap it in an array in PHP:

echo json_encode(array($data));

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133390

you could use an array

$myArray[]['No'] = "0001";
$myArray[]['Address'] = "B BLOCK 2";

echo json_encode($myArray);

Upvotes: 0

Related Questions