$arr = array (
array(
'type' => 'Tea', 'amount' => $expense_tea, 'desp' => $expense_tea_desc
),
array(
'type' => 'BusFare', 'amount' => $expense_bus, 'desp' => $expense_bus_desc
),
array(
'type' => 'Food', 'amount' => $expense_food, 'desp' => $expense_food_desc
)
);
$myObj1->expensedetails = json_encode($arr);
$json1 = json_encode($myObj1);
Description
- I have tried to create a nested json array using php
The Output:
{
"expensedetails": {
"[{\"type\":\"Tea\",\"amount\":\"0\",\"desp\":\"0\"},{\"type\":\"BusFare\",\"amount\":\"0\",\"desp\":\"0\"},{\"type\":\"Food\",\"amount\":\"0\",\"desp\":\"0\"}]"
}
}
Explanation
- The json has been converted to string
Expected Output
{
"expensedetails":
[
{"type":"Tea","amount":"0","desp":"0"},
{"type":"BusFare","amount":"0","desp":"0"},
{"type":"Food","amount":"0","desp":"0"},
{"type":"SalaryAdvance","amount":"0","desp":"0"},
{"type":"OT","amount":"0","desp":"0"},
{"type":"IceFlakes","amount":"0","desp":"0"}
]
}
Conclusion
- I need a code like in the above-expected code output
- But when I tried to do nested json array