PHP数组处理$arr1转换为$arr2

请编写一段程序将$arr1转换为$arr2

$arr1 = array(
'0'=>array ('fid'=>1,'tid'=>1,'name'=>'Name1'),
'1'=>array ('fid'=>2,'tid'=>2,'name'=>'Name2'),
'2'=>array ('fid'=>3,'tid'=>5,'name'=>'Name3'),
'3'=>array ('fid'=>4,'tid'=>7,'name'=>'Name4'),
'4'=>array ('fid'=>5,'tid'=>9,'name'=>'Name5'),
);

$arr2 = array (
    '1'=> array(
        '0' => array ('tid' => 1, 'name' => 'Name1'),
        '1'=> array ( 'tid' => 2, 'name' => 'Name2'),
        '2' => array ( 'tid' => 5, 'name' => 'Name3'),
        '3' => array ( 'tid' => 7, 'name' => 'Name4' ),
        '4' => array ('tid' => 9, 'name' => 'Name5' )
        )
    );

方法1:

        $arr2 = [];
foreach ($arr1 as $key=>$value){
    unset($value['fid']);
    $arr2[1][] = $value;
        }
     print_r($arr2);

方法2:

        foreach ($arr1 as $key=>$value){
            $item[$value['fid']][] = array(
                'tid'=>$value['tid'],
                'name'=>$value['name'],
            );
        }
        foreach ($item as $v){
            $arr2[] = $v;
        }
        var_dump($arr2);die();

你可能感兴趣的:(php,开发语言)