您好 我想转换此数组,其中id为键和名称值对
Array(
[4882] => treatment
[4876] => Advance
[4854] => Applied Clinical
)
到
Array(
[0] => Array([id] => 4882, [name] => treatment)
[1] => Array([id] => 4876, [name] => Advance)
[2] => Array([id] => 4854, [name] => Applied Clinical)
)
答案 0 :(得分:3)
foreach ($original_array as $key=>$value){
$new_arrays[] = array('id'=>$key,'name'=>$value);
}
print_r($new_arrays);
答案 1 :(得分:2)
你可以这样做:
$output_arr = array();
foreach($input_arr as $key => $value)
$output_arr[] = array('id' => $key, 'name' => $value);
然后你可以使用$ output_arr作为转换数组或将其内容分配给$ input_arr:
$input_arr = &$output_arr;
答案 2 :(得分:2)
$input
是您的第一个数组,$output
将保存结果数组
$output = array();
foreach ($input as $id => $name)
{
$output[] = array('id'=>$id, 'name'=>$name);
}