i need this json format with php code
is there any way to generate JSON like this with PHP ?
[
{
"name":"Steve",
"company":"Apple"
},
{
"name":"Bill",
"company":"Microsoft"
}
]
can anyone help ?
Do you want to do this:
$a = array();
$b = array('name' => '', 'company' => '');
$b['name'] = 'Steve';
$b['company'] = 'Apple';
$a[]= $b;
$b['name'] ='Bill';
$b['company']='Microsoft';
$a[]= $b;
echo json_encode($a);
This will give you
[
{
"name": "Steve",
"company": "Apple"
},
{
"name": "Bill",
"company": "Microsoft"
}
]
Try this:
json_encode($variableName);
use json_encode
$var = json_encode($array);
You can use json_encode
# To Output
die(json_encode($myarray));
# To Store
$myJson = json_encode($myarray);
Just a note on json_encode you will need to add the square brackets if your code requires it.
echo '['.json_encode(array('index1'=>1)).']';
You can use json_encode
like this:
$my_data = array(
array(
'name' => 'Steve',
'company' => 'Apple'
),
array(
'name' => 'Bill',
'company' => 'Microsoft'
)
);
echo json_encode($my_data);
It does not matter that both of the inner arrays contain the same keys because they are still in separate arrays and will form properly when encoded in JSON.
This
$array = array (
array ('name' => 'Steve', 'company' => 'Apple', ),
array ('name' => 'Bill', 'company' => 'Microsoft', ),
);
$result = json_encode($array);
And this
$person_steve = new stdClass;
$person_steve->name = 'Steve';
$person_steve->company = 'Apple';
$person_bill = new stdClass;
$person_bill->name = 'Bill';
$person_bill->company = 'Microsoft';
$array = array ($person_steve, $person_bill);
$result = json_encode($array);