php重组数据组,php数组分组
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('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将二维数组重新组合
//这个问题也就看起来麻烦一点.我给你写一个
//假设你上面的数组是$arrOld.
//$arrReturn作为结果数组.
$arrReturn = [];
foreach($arrReturn as $value) {
foreach($value as $key = $val) {
$arrReturn[$key][] = $val;
}
}
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 数组重组
很简单,代码如下:
// 假设数组变量$array保存了当前数据
foreach ($array as $key $value) { // 遍历数组
// 通过正则表达式来判断是否为诸如1.x的下标
if (!preg_match('/\./', $key)) {
// 删除数组诸如下标为:1,2,3的数据
unset($array[$key]);
continue;
}
// 通过.点来分割诸如1.x的下标
$array2 = explode('.', $key);
// 使用新数组来存储数据
$array3[$array2[0]][$key] = $value;
}
// 如果数组下标在排序上有任何颠倒可用ksort()[正序]或krsort()[倒序]来排序
ksort($array3);
// 打印重组的数组效果如下
print_r($array3);
Array
(
[1] = Array
(
[1.1] = 美妆保养
[1.2] = 美容工具
[1.3] = 护肤保养
[1.4] = 美容化妆
[1.5] = 美发护发
)
[2] = Array
(
[2.1] = 香水须后水
[2.2] = 工具配件
)
)
如果还有任何问题,欢迎追问~
网页标题:php重组数据组,php数组分组
URL地址:http://azwzsj.com/article/dsipjgi.html