当前位置:首页 » 编程语言 » php数组转置

php数组转置

发布时间: 2024-12-31 01:34:09

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
)

)

热点内容
hibernatesql 发布:2025-01-02 23:12:22 浏览:674
苹果指纹密码忘了怎么办 发布:2025-01-02 23:07:09 浏览:52
安卓线性布局怎么改 发布:2025-01-02 22:50:38 浏览:533
数据存储企业 发布:2025-01-02 22:10:11 浏览:147
数据库收缩日志 发布:2025-01-02 22:10:07 浏览:18
解压游戏越狱 发布:2025-01-02 21:55:52 浏览:703
换机助手文件夹 发布:2025-01-02 21:49:07 浏览:731
mac以点开头的文件夹 发布:2025-01-02 21:42:17 浏览:520
计划哥脚本 发布:2025-01-02 21:40:41 浏览:205
QQ编程语 发布:2025-01-02 21:40:33 浏览:664