php數組轉置
❶ php二維數組轉一維數組
其實這是一個數組轉置演算法,是很常用的演算法,例子程序代碼如下:
<?php
$arr=array(
'oneid' => array ('4','5'),
'id' => array ('4','5'),
'Rname' => array ('商人' ,'VIP' ),
'R0' => array ('30000' ,'100001' ),
'R1' => array ('100000' ,'5000000' ),
'chkall' => 'on');
echo "原始數組:\n";
print_r($arr);
$arr2=array();
foreach ($arr as $k=>$v) {
if (is_array($v)) {
foreach ($v as $k2=>$v2) $arr2[$k2][$k]=$v2;
}
}
echo "轉換後的數組:\n";
print_r($arr2);
?>
以上程序的運行結果為:
原始數組:
Array
(
[oneid] => Array
(
[0] => 4
[1] => 5
)
[id] => Array
(
[0] => 4
[1] => 5
)
[Rname] => Array
(
[0] => 商人
[1] => VIP
)
[R0] => Array
(
[0] => 30000
[1] => 100001
)
[R1] => Array
(
[0] => 100000
[1] => 5000000
)
[chkall] => on
)
轉換後的數組:
Array
(
[0] => Array
(
[oneid] => 4
[id] => 4
[Rname] => 商人
[R0] => 30000
[R1] => 100000
)
[1] => Array
(
[oneid] => 5
[id] => 5
[Rname] => VIP
[R0] => 100001
[R1] => 5000000
)
)