# 将这两个数组用PHP弄成最后的样子，最优化的方法~~

\$cat = array(12=>array('catname'=>'xxxx'),13->array('catname'=>'bbbb'));

0

```\$res = array();
foreach (\$cat as \$key =>\$val) {
\$res[] = array(
'catid' => \$key,
'catname' => \$val['catname'],
);
}```

0
```方法一（array_map）：
\$cat = array(12=>array('catname'=>'xxxx'),13=>array('catname'=>'bbbb'));
\$cat = array_map('structArray', \$menu, \$cat);
function structArray(\$key, \$val){
\$val['catid'] = \$key;
return \$val;
}
print_r(\$cat);

\$cat = array(12=>array('catname'=>'xxxx'),13=>array('catname'=>'bbbb'));
array_walk(\$cat, 'structArray');
function structArray(&\$val, \$key){
\$val['catid'] = \$key;
}
print_r(\$cat);```
0

#### 引用来自“Frank_mc”的答案

```\$res = array();
foreach (\$cat as \$key =>\$val) {
\$res[] = array(
'catid' => \$key,
'catname' => \$val['catname'],
);
}```

```foreach( \$cat as \$key=>&\$row ){
\$row['catid'] = \$key;
}```

0
```\$menu = array(12,13);
\$cat = array(12=>array('catname'=>'xxxx'), 13=>array('catname'=>'bbbb'));

\$res = array();
foreach(\$cat as \$k => \$v){