PHP二维数组转一维数组
PHP二维数组转一维数组,array(size=6)'oneid'=>array(size=2)0=>string'4'1=>string'5''id'=>array(s...
PHP二维数组转一维数组,array (size=6)
'oneid' =>
array (size=2)
0 => string '4'
1 => string '5'
'id' =>
array (size=2)
0 => string '4'
1 => string '5'
'Rname' =>
array (size=2)
0 => string '商人'
1 => string 'VIP'
'R0' =>
array (size=2)
0 => string '30000'
1 => string '100001'
'R1' =>
array (size=2)
0 => string '100000'
1 => string '5000000'
'chkall' => string 'on'
将上面的二维数组转为类似下面的多个一维数据
array (size=5)
'oneid' => string '5'
'id' => string '5' (length=1)
'Rname' => string 'VIP'
'R0' => string '100001'
'R1' => string '5000000'
array (size=5)
'oneid' => string '4'
'id' => string '4'
'Rname' => string '商人'
'R0' => string '30000'
'R1' => string '100000' 展开
'oneid' =>
array (size=2)
0 => string '4'
1 => string '5'
'id' =>
array (size=2)
0 => string '4'
1 => string '5'
'Rname' =>
array (size=2)
0 => string '商人'
1 => string 'VIP'
'R0' =>
array (size=2)
0 => string '30000'
1 => string '100001'
'R1' =>
array (size=2)
0 => string '100000'
1 => string '5000000'
'chkall' => string 'on'
将上面的二维数组转为类似下面的多个一维数据
array (size=5)
'oneid' => string '5'
'id' => string '5' (length=1)
'Rname' => string 'VIP'
'R0' => string '100001'
'R1' => string '5000000'
array (size=5)
'oneid' => string '4'
'id' => string '4'
'Rname' => string '商人'
'R0' => string '30000'
'R1' => string '100000' 展开
2个回答
展开全部
其实这是一个数组转置算法,是很常用的算法,例子程序代码如下:
<?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
)
)
<?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
)
)
展开全部
PHP二维数组转为一维数组的方法实例,分别使用foreach、for、while三种循环方法,还可以使用PHP自带的array_column()函数;
具体示例:
二维数组
$msg = array(
array(
'id'=>'45',
'name'=>'jack'
),
array(
'id'=>'34',
'name'=>'mary'
),
array(
'id'=>'78',
'name'=>'lili'
),
);
第一种方法:
foreach($msg as $k => $v){
$ids[] = $id;
$names[] = $name;
}
第二种方法:
<?php
$ids = array_column($msg, 'id');
$names = array_column($msg, 'name');
?>
注意:array_column();可以有第三个参数,如 $n = array_column($msg, 'name', 'id');
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询