#php
#php
Вопрос:
У меня есть следующие массивы :
Array (
[0] => Array (
[id] => 1
[name] => Apple
[type] => Fruit
)
[1] => Array (
[id] => 3
[name] => Chanterelles
[type] => Vegetable
)
[2] => Array (
[id] => 4
[name] => Lenses
[type] => Legume
)
[3] => Array (
[id] => 2
[name] => Banana
[type] => Fruit
)
[4] => Array (
[id] => 5
[name] => Rocket
[type] => Vegetable
)
)
Мой ожидаемый результат :
Array (
[Fruit] => Array (
Array (
[id] => 1
[name] => Apple
)
Array (
[id] => 2
[name] => Banana
)
)
[Vegetable] => Array (
Array (
[id] => 3
[name] => Chanterelles
)
Array (
[id] => 5
[name] => Rocket
)
)
[Legume] => Array (
Array (
[id] => 4
[name] => Lenses
)
)
)
Мой исходный массив :
var_dump(array(
array(
'id' => 1,
'name' => 'Apple',
'type' => 'Fruit'
),
array(
'id' => 3,
'name' => 'Chanterelles',
'type' => 'Vegetable'
),
array(
'id' => 4,
'name' => 'Lenses',
'type' => 'Legume'
),
array(
'id' => 2,
'name' => 'Banana',
'type' => 'Fruit'
),
array(
'id' => 5,
'name' => 'Rocket',
'type' => 'Vegetable'
)
));
Я попробовал следующее :
foreach($myarray as $ingredient){
$type_arr[] = $ingredient['type'];
}
$type_arr_uq = array_unique($type_arr);
$new_arr = array();
foreach($type_arr_uq as $type){
foreach($myarray as $ingredient){
if($type == $ingredient['type']){
$new_arr[$type] = ['id' => $ingredient['id'], 'name' => $ingredient['name']];
}
}
}
Вывод:
Array
(
[Fruit] => Array
(
[id] => 2
[name] => Banana
)
[Vegetable] => Array
(
[id] => 5
[name] => Rocket
)
[Legume] => Array
(
[id] => 4
[name] => Lenses
)
)
Демонстрация: http://sandbox.onlinephpfunctions.com/code/4886faa73b56b62fdf954f06e902f1d0bd301a71
Ответ №1:
$new_arr[$type] = ['id' => $ingredient['id'], 'name' => $ingredient['name']];
Вы перезаписываете потенциально уже существующую запись массива в этом месте — вместо этого добавьте новую:
$new_arr[$type][] = ['id' => $ingredient['id'], 'name' => $ingredient['name']];
Ответ №2:
Как насчет такого подхода?
<?php
function groupArray($array, $key) {
$return = array();
foreach($array as $val) {
$return[$val[$key]][] = $val;
}
return $return;
}
$myArray = array(
array(
'id' => 1,
'name' => 'Apple',
'type' => 'Fruit'
),
array(
'id' => 3,
'name' => 'Chanterelles',
'type' => 'Vegetable'
),
array(
'id' => 4,
'name' => 'Lenses',
'type' => 'Legume'
),
array(
'id' => 2,
'name' => 'Banana',
'type' => 'Fruit'
),
array(
'id' => 5,
'name' => 'Rocket',
'type' => 'Vegetable'
)
);
print_r(groupArray($myArray, 'type'));
?>
вывод :
Array ( [Fruit] => Array ( [0] => Array ( [id] => 1 [name] => Apple [type] => Fruit ) [1] => Array ( [id] => 2 [name] => Banana [type] => Fruit ) ) [Vegetable] => Array ( [0] => Array ( [id] => 3 [name] => Chanterelles [type] => Vegetable ) [1] => Array ( [id] => 5 [name] => Rocket [type] => Vegetable ) ) [Legume] => Array ( [0] => Array ( [id] => 4 [name] => Lenses [type] => Legume ) ) )
Ответ №3:
Я могу посоветовать функцию array_reduce:
$groupped_array = array_reduce(
$myarray,
function($carry, $item) {
if (!isset($carry[$item['type']])) $carry[$item['type']] = [];
array_push($carry[$item['type']], [$item['id'], $item['name']]);
return $carry;
},
[]
);
Тест в PHP sandbox
Ответ №4:
Очень простой пример:
$result = [];
foreach ($array as $value) {
$type = $value['type'];
unset($value['type']);
$result[$type][] = $value;
}