php 怎样把二维数组转为一维数组
3个回答
展开全部
array_column这个函数应该能满足你的需求
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$first_names = array_column($records, 'first_name');
var_dump($first_names);
会输出
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$arr=array(
'0'=>array(
'a'=>2,
'b'=>'fd',
'c'=>'fgd',
'd'=>'hg',
),
'1'=>array(
'e'=>2,
'b'=>'fd',
'f'=>'fgd',
),
'2'=>array(
'a'=>2,
'c'=>'fd',
'e'=>'fgd',
'b'=>'hg',
),
'3'=>array(
'c'=>2,
'd'=>'fd',
'e'=>'fgd',
'f'=>'hg',
),
);
foreach($arr as $k=>$v){
foreach($v as $key=>$val){
$arr2[$key]=$val;
}
}
'0'=>array(
'a'=>2,
'b'=>'fd',
'c'=>'fgd',
'd'=>'hg',
),
'1'=>array(
'e'=>2,
'b'=>'fd',
'f'=>'fgd',
),
'2'=>array(
'a'=>2,
'c'=>'fd',
'e'=>'fgd',
'b'=>'hg',
),
'3'=>array(
'c'=>2,
'd'=>'fd',
'e'=>'fgd',
'f'=>'hg',
),
);
foreach($arr as $k=>$v){
foreach($v as $key=>$val){
$arr2[$key]=$val;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?php
$arr = array(
0 => array(
0 => array(
'id' => 5,
'name' => 'aaa',
'times' => 5
),
1 => array(
'id' => 5,
'name' => 'aaa',
'times' => 5
),
),
1 => array(
0 => array(
'id' => 5,
'name' => 'aaa',
'times' => 5
),
1 => array(
'id' => 5,
'name' => 'aaa',
'times' => 5
),
),
);
$arr2 = array();
foreach ($arr as $k => $v) {
foreach ($v as $m => $n) {
$arr2[0][] = $n;
}
}
echo "<pre>";
print_r($arr2);
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询