请编写一段程序将$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:文章来源:https://www.toymoban.com/news/detail-685973.html
$arr2 = [];
foreach ($arr1 as $key=>$value){
unset($value['fid']);
$arr2[1][] = $value;
}
print_r($arr2);
方法2:文章来源地址https://www.toymoban.com/news/detail-685973.html
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数组处理$arr1转换为$arr2的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!