php 把多维数组中,某值相同的项合并,重组数据
?php
创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的乐安网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
$laArray = array(
0 = array("id"=11,"name"="happy","age"="20","moeny"=100),
1 = array("id"=29,"name"="lucy","age"="20","moeny"=100),
2 = array("id"=34,"name"="happy","age"="20","moeny"=235),
3 = array("id"=42,"name"="happy","age"="15","moeny"=100),
4 = array("id"=51,"name"="happy","age"="20","moeny"=600),
5 = array("id"=61,"name"="lucy","age"="20","moeny"=350),
6 = array("id"=61,"name"="lucy","age"="23","moeny"=150),
);
$temArr = $laArray ;
$newArr = array();
foreach($laArray as $num = $arr ){
unset($temArr[$num]);
$id = $arr['id'];
foreach($temArr as $tNum =$tArr ){
$n_arr = array();
$tem_moeny = array();
if($arr['name']== $tArr['name'] $arr['age']== $tArr['age']){
$id .= '+'.$tArr['id'];
$n_arr['id'] = $id;
$n_arr['name'] = $arr['name'];
$n_arr['age'] = $arr['age'];
if(is_array($arr['moeny'])){
array_push($n_arr['moeny'], $tArr['moeny']);
}else{
$tem_moeny [] = $arr['moeny'];
$tem_moeny [] = $tArr['moeny'];
}
$tem_moeny[] = $tArr['moeny'];
$n_arr['moeny'] = $tem_moeny;
$newArr[] = $n_arr ;
}
}
if($id == $arr['id']){
$newArr[] = $arr ;
}
}
print_r($newArr);
?
PHP 一个数组中 根据某个相同的值 将不同的值重组一个新数组
例子:
复制代码 代码如下:
?php
$array1 = array("color" = "red", 2, 4);
$array2 = array("a", "b", "color" = "green", "shape" = "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?
上例将输出:
复制代码 代码如下:
Array
(
[color] = green
[0] = 2
[1] = 4
[2] = a
[3] = b
[shape] = trapezoid
[4] = 4
)
如何将 php 二维数组重组
?php
$a=array(
0 = array(
"yw" ="85",
"sx" ="97",
"yy" ="100"
),
1= array(
"yw"= "85.5",
"sx" = "96",
"yy"= "96"
),
2= array(
"yw" = "84.5",
"sx" = "90",
"yy" = "98",
)
);
$f=array();
foreach($a as $b=$c){
foreach($c as $d=$e){
$f[$d][]=$e;
}
}
echo 'pre/';
print_r($f);
?
php数组重组
是固定的么?
?php
$a = array('0' = array('id' = 151,'name'='leo'),
'1' = array('id' = 120,'name'='john'),
'2' = array('id' = 153,'name'='hello'),
'3' = array('id' = 118,'name'='word'),
);
foreach($a as $k = $v){
if($v['id']==151 || $v['id']==120){
$b['0'][] = $a[$k];
}else{
$b['1'][] = $a[$k];
}
}
var_dump($b);
网站名称:数据的重组php 数据重组强调
文章链接:http://scyingshan.cn/article/doojehs.html