The code that I have does not display what I need, tell me what I’m doing wrong
$new_array =[];
foreach($result as $row)
{
$array =[
'id'=> $row["id"],
'summ' => $row["summ"],
];
foreach($array AS $key => $val) {
$new_array[] = $val;
}
}
echo json_encode($new_array);
Outputs the following result
["26","180","25","35","24","50","23","50","22","100"]
But I need the result to be different, and I can’t.
Here’s an example of how it should be:
[
{"26","180"},
{"25","35"},
{"24","50"},
{"23","50"},
{"22","100"}
]
Please tell me how to achieve this?
>Solution :
You can skip the inner loop:
$new_array = [];
foreach($result as $row)
{
$new_array[] = [
$row['id'],
$row['summ']
];
}
echo json_encode($new_array);
That should give you the result:
[
["26","180"],
["25","35"],
...
]