PHP通过键和值创建数组组



我在下面的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
)

)
)    

有人能有效地做到这一点吗?。请帮帮我感谢

尝试此代码

<?php
$arr = 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'
)
);
echo "<pre>";
$newArr = array();
foreach($arr as $k=>$v){
$newArr[$v['video_category_name']]['ctg_name']=$v['video_category_name'];
$newArr[$v['video_category_name']]['cat_id']=$v['video_category'];
$newArr[$v['video_category_name']]['data'][]=$v;
}
$newArr1 = array();
foreach($newArr as $k=>$v){
$newArr1[] = $v;
}
print_r($newArr);
?>

输出

Array
(
[Beginner] => 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
)
)
)
[Mobility] => 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;
}
$final_arr = [];
foreach($result as $val){
$final_arr[] = [
'ctg_name'=>$val[0]['video_category_name'],
'cat_id'=>$val[0]['video_category'],
'data'=>$val
]
}

相关内容

  • 没有找到相关文章

最新更新