У меня есть массив в коде PHP ниже, и я хочу преобразовать этот массив для группировки по значению данных. Всегда сложно упростить массивы
Array
(
[0] => Array
(
[video_id] => 14
[video_title] => test1
[video_category_name] => Beginner
[video_category] => 1
)
[1] => Array
(
[video_id] => 18
[video_title] => test
[video_category] => 1
[video_category_name] => Beginner
)
[2] => Array
(
[video_id] => 17
[video_title] => first
[video_category] => 3
[video_category_name] => Mobility
)
[3] => Array
(
[video_id] => 19
[video_title] => second
[video_category] => 3
[video_category_name] => Mobility
)
)
И я ожидаю этот массив:
(
[0] => Array
(
[ctg_name] => Beginner
[cat_id] => 1
[ data] => Array
(
(
[0] => Array
(
[video_id] => 14
[video_title] => test1
[video_category_name] => Beginner
[video_category] => 1
)
[1] => Array
(
[video_id] => 18
[video_title] => test
[video_category] => 1
[video_category_name] => Beginner
)
)
)
[1] => Array
(
[ctg_name] => Mobility
[cat_id] => 3
[ data] => Array
(
[0] => Array
(
[video_id] => 17
[video_title] => first
[video_category] => 3
[video_category_name] => Mobility
)
[1] => Array
(
[video_id] => 19
[video_title] => second
[video_category] => 3
[video_category_name] => Mobility
)
)
)
)
Я попробовал это, но не получил ожидаемого результата.
foreach($video as $val){
$result[$val['video_category']][] = $val;
}
Возвращается так:
Array
(
[1] => Array
(
[0] => Array
(
[video_id] => 14
[video_title] => test1
[video_category_name] => Beginner
[video_category] => 1
)
[1] => Array
(
[video_id] => 18
[video_title] => test
[video_category] => 1
[video_category_name] => Beginner
)
)
[3] => Array
(
[0] => Array
(
[video_id] => 17
[video_title] => first
[video_category] => 3
[video_category_name] => Mobility
)
[1] => Array
(
[video_id] => 19
[video_title] => second
[video_category] => 3
[video_category_name] => Mobility
)
)
)
У кого-нибудь есть эффективный способ сделать это?
пожалуйста, помогите мне
Спасибо
Array
(
[0] => Array
(
[video_id] => 14
[video_title] => test1
[video_category_name] => Beginner
[video_category] => 1
)
[1] => Array
(
[video_id] => 18
[video_title] => test
[video_category] => 1
[video_category_name] => Beginner
)
[2] => Array
(
[video_id] => 17
[video_title] => first
[video_category] => 3
[video_category_name] => Mobility
)
[3] => Array
(
[video_id] => 19
[video_title] => second
[video_category] => 3
[video_category_name] => Mobility
)
)
И я ожидаю, что этот массив:
(
[0] => Array
(
[ctg_name] => Beginner
[cat_id] => 1
[ data] => Array
(
(
[0] => Array
(
[video_id] => 14
[video_title] => test1
[video_category_name] => Beginner
[video_category] => 1
)
[1] => Array
(
[video_id] => 18
[video_title] => test
[video_category] => 1
[video_category_name] => Beginner
)
)
)
[1] => Array
(
[ctg_name] => Mobility
[cat_id] => 3
[ data] => Array
(
[0] => Array
(
[video_id] => 17
[video_title] => first
[video_category] => 3
[video_category_name] => Mobility
)
[1] => Array
(
[video_id] => 19
[video_title] => second
[video_category] => 3
[video_category_name] => Mobility
)
)
)
)
Я попробовал это, но не получил ожидаемого результата.
foreach($video as $val){
$result[$val['video_category']][] = $val;
}
Возвращается так:
Array
(
[1] => Array
(
[0] => Array
(
[video_id] => 14
[video_title] => test1
[video_category_name] => Beginner
[video_category] => 1
)
[1] => Array
(
[video_id] => 18
[video_title] => test
[video_category] => 1
[video_category_name] => Beginner
)
)
[3] => Array
(
[0] => Array
(
[video_id] => 17
[video_title] => first
[video_category] => 3
[video_category_name] => Mobility
)
[1] => Array
(
[video_id] => 19
[video_title] => second
[video_category] => 3
[video_category_name] => Mobility
)
)
)
У кого-нибудь есть эффективный способ сделать это?
пожалуйста, помогите мне
Спасибо