Использовать имя в массиве в качестве группы (ключа) многомерного массива.
$array = json_decode('[
{"id":14970,
"amount":"70",
"name":"Food"
},
{"id":14970,
"amount":"100",
"name":"Drink"
},
{"id":14970,
"amount":"100",
"name":"Food"
},
{"id":14970,
"amount":"300",
"name":"Drink"
},
{"id":14970,
"amount":"10",
"name":"Taxi"
},
{"id":14970,
"amount":"200",
"name":"Food"
}
]',true);
$result = [];
foreach($array as $key=>$value){
$group = $value['name'];
if(!isset($result[$group])) $result[$group ] = [];
$result[$group][] = $value;
}
//remove the top level keys, it's easier to build it with them
$result = array_values($result);
Выход
array (
0 =>
array (
0 =>
array (
'id' => 14970,
'amount' => '70',
'name' => 'Food',
),
1 =>
array (
'id' => 14970,
'amount' => '100',
'name' => 'Food',
),
2 =>
array (
'id' => 14970,
'amount' => '200',
'name' => 'Food',
),
),
1 =>
array (
0 =>
array (
'id' => 14970,
'amount' => '100',
'name' => 'Drink',
),
1 =>
array (
'id' => 14970,
'amount' => '300',
'name' => 'Drink',
),
),
2 =>
array (
0 =>
array (
'id' => 14970,
'amount' => '10',
'name' => 'Taxi',
),
),
)
Песочница
Тогда вам просто нужно json_encode это.
$result = [];
foreach($array as $key=>$value){
$group = $value['name'];
if(!isset($result[$group])) $result[$group ] = [];
$result[$group][] = $value;
}
echo json_encode(array_keys($result), JSON_PRETTY_PRINT);
Что дает:
[
[
{
"id": 14970,
"amount": "70",
"name": "Food"
},
{
"id": 14970,
"amount": "100",
"name": "Food"
},
{
"id": 14970,
"amount": "200",
"name": "Food"
}
],
[
{
"id": 14970,
"amount": "100",
"name": "Drink"
},
{
"id": 14970,
"amount": "300",
"name": "Drink"
}
],
[
{
"id": 14970,
"amount": "10",
"name": "Taxi"
}
]
]
Песочница