本文目录一览:
- 1、php数据数组重新分组
- 2、如何实现PHP根据数组的值进行分组
- 3、php 数组 分组求和
- 4、PHP根据二维数组元素数量拆分成若干个小数组?
- 5、php 数组【1,2,3,4,5,6,7,8,9,10】分组
php数据数组重新分组
$arr = array(
"圆柱加" = array(
"data" = array(
array(
"product_id" = 36,
"style_id" = 102,
"style_name" = "圆柱架",
"product_sn" = "MMJ000036",
"product_thumb" = "a.jpg"
),
array(
"product_id" = 36,
"style_id" = 102,
"style_name" = "圆柱架",
"product_sn" = "MMJ000036",
"product_thumb" = "a.jpg"
),
)
),
"蝴蝶夹" = array(
"data" = array(
array(
"product_id" = 36,
"style_id" = 102,
"style_name" = "蝴蝶夹",
"product_sn" = "MMJ000036",
"product_thumb" = "a.jpg"
),
array(
"product_id" = 36,
"style_id" = 102,
"style_name" = "蝴蝶夹",
"product_sn" = "MMJ000036",
"product_thumb" = "a.jpg"
),
)
)
);
$count = 0;
foreach($arr as $k=$v){
$arr[$k]['group_name'] = $k;
$arr[$count] = $arr[$k];
unset($arr[$k]);
$count++;
}
echo json_encode($arr);
这样输出出来格式是"data":[{}],"group_name": "XXX"
反正是json 反的也没问题吧?
写这么多你都不给我采纳一下。。。
如何实现PHP根据数组的值进行分组
PHP根据数组的值分组,php array中没有自带这个函数但是很常用
代码:
$_array = array(
array(1,11,'2016-05-18'),
array(2,11,'2016-05-18'),
array(3,22,'2016-05-18'),
array(4,22,'2016-05-18'),
array(5,33,'2016-05-19'),
array(6,33,'2016-05-19'),
array(7,44,'2016-05-19'),
array(8,44,'2016-05-19'),
array(9,55,'2016-05-20'),
array(10,55,'2016-05-20'),
);
var_dump(array_val_chunk($_array));
function array_val_chunk($array){
$result = array();
foreach ($array as $key = $value) {
$result[$value[1].$value[2]][] = $value;
}
$ret = array();
//这里把简直转成了数字的,方便同意处理
foreach ($result as $key = $value) {
array_push($ret, $value);
}
return $ret;
}
运行结果如下:
array(5) {
[0]=
array(2) {
[0]=
array(3) {
[0]=
int(1)
[1]=
int(11)
[2]=
string(10) "2016-05-18"
}
[1]=
array(3) {
[0]=
int(2)
[1]=
int(11)
[2]=
string(10) "2016-05-18"
}
}
[1]=
array(2) {
[0]=
array(3) {
[0]=
int(3)
[1]=
int(22)
[2]=
string(10) "2016-05-18"
}
[1]=
array(3) {
[0]=
int(4)
[1]=
int(22)
[2]=
string(10) "2016-05-18"
}
}
[2]=
array(2) {
[0]=
array(3) {
[0]=
int(5)
[1]=
int(33)
[2]=
string(10) "2016-05-19"
}
[1]=
array(3) {
[0]=
int(6)
[1]=
int(33)
[2]=
string(10) "2016-05-19"
}
}
[3]=
array(2) {
[0]=
array(3) {
[0]=
int(7)
[1]=
int(44)
[2]=
string(10) "2016-05-19"
}
[1]=
array(3) {
[0]=
int(8)
[1]=
int(44)
[2]=
string(10) "2016-05-19"
}
}
[4]=
array(2) {
[0]=
array(3) {
[0]=
int(9)
[1]=
int(55)
[2]=
string(10) "2016-05-20"
}
[1]=
array(3) {
[0]=
int(10)
[1]=
int(55)
[2]=
string(10) "2016-05-20"
}
}
}
php 数组 分组求和
?php
//多维数组
$arr = array(
array('goods_id'=1888,'provider_id'=81,'in_number'=20,'out_number'=30),
array('goods_id'=1888,'provider_id'=8818,'in_number'=10,'out_number'=20),
array('goods_id'=198,'provider_id'=894,'in_number'=10,'out_number'=20)
);
//定义新数组接收元素
$newArr = array();
if($arr[0]['goods_id'] == $arr[1]['goods_id']){
$newArr['in_number'] = $arr[0]['in_number'] + $arr[1]['in_number'];
$newArr['provider_id'] = $arr[0]['provider_id'] $arr[1]['provider_id'] ? $arr[0]['provider_id'] : $arr[1]['provider_id'];
$newArr['out_number'] = $arr[0]['out_number'] $arr[1]['out_number'] ? $arr[0]['out_number'] : $arr[1]['out_number'];
}elseif($arr[0]['goods_id'] == $arr[2]['goods_id']){
$newArr['in_number'] = $arr[0]['in_number'] + $arr[2]['in_number'];
$newArr['provider_id'] = $arr[0]['provider_id'] $arr[2]['provider_id'] ? $arr[0]['provider_id'] : $arr[2]['provider_id'];
$newArr['out_number'] = $arr[0]['out_number'] $arr[2]['out_number'] ? $arr[0]['out_number'] : $arr[2]['out_number'];
}elseif($arr[1]['goods_id'] == $arr[2]['goods_id']){
$newArr['in_number'] = $arr[1]['in_number'] + $arr[2]['in_number'];
$newArr['provider_id'] = $arr[1]['provider_id'] $arr[2]['provider_id'] ? $arr[1]['provider_id'] : $arr[2]['provider_id'];
$newArr['out_number'] = $arr[1]['out_number'] $arr[2]['out_number'] ? $arr[1]['out_number'] : $arr[2]['out_number'];
}
echo 'pre';
var_dump($newArr);
/*输出如下
array(3) {
["in_number"]=
int(30)
["provider_id"]=
int(81)
["out_number"]=
int(30)
}
*/
PHP根据二维数组元素数量拆分成若干个小数组?
php数组分组可以使用函数array_chunk,按照给定的规则进行遍历,如果数组的num值大于8,就整除8,获取整除的数据,然后按照整除数进行分组分块即可。
php 数组【1,2,3,4,5,6,7,8,9,10】分组
array_chunk(array,size,preserve_keys) 函数把一个数组分割为新的数组块。
array 必需。规定要使用的数组。
size 必需。一个整数,规定每个新数组块包含多少个元素。
preserve_key 可选。可能的值:
true - 保留原始数组中的键名。
false - 默认。每个新数组块使用从零开始的索引。
求采纳,谢谢!