php合并相同数据合并 php合并图片
php 数组相同的元素组合并在一起
具体实例代码如下:
我们提供的服务有:成都网站建设、成都网站设计、微信公众号开发、网站优化、网站认证、长沙县ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的长沙县网站制作公司
?php
// 删除数组中相同元素,只保留一个相同元素
function formatArray($array)
{
sort($array);
$tem = "";
$temarray = array();
$j = 0;
for($i=0;$icount($array);$i++)
{
if($array[$i]!=$tem)
{
$temarray[$j] = $array[$i];
$j++;
}
$tem = $array[$i];
}
return $temarray;
}
//测试 调用函数
$array = array('aa','bb','aa',3,4,5,5,5,5,'bc');
$arr = formatArray($array);
print_r($arr);
?
PHP如何合并数组当中有相同数值的数组
array_unique函数提供了过滤重复的值的功能,但是该函数只能处理一维数组,如果遇到多维数组我们可以现将其转化为一维数组。示例代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* $data 是你要处理数据
* $result 是输出的结果
*/
//将其转化为一维数组
foreach ($data as $row) {
$row = serialize($row);
}
unset($row);
$data = array_unique($data);
//还原多维数组
foreach ($data as $row) {
$row = unserialize($row);
}
unset($row);
不好意思 楼主要求,我看错了,追加下面的代码
1
2
3
4
5
6
7
8
9
10
11
12
$result=array();
$tmp_arr=array();
foreach ($data as $row) {
$tmp_arr[$row['confuguarable_sku']][] = $row['size'];
}
//将尺寸用逗号拼接
foreach ($tmp_arr as $sku = $size) {
$result[] = array('confuguarable_sku'=$sku, 'size'=implode(",", $size));
}
echo "pre";
//输出结果
print_r($result);
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);
?
thinkphp中两条数据有相同的内容,如何将他们合并成一条数据
1. 在用mysql查询的时候,用group by (某一个字段) 将其查询结果合并;
或者;
2. 查询条件用limit 1 限制查询结果条数,反正内容一样,你就要一条即可;
本文名称:php合并相同数据合并 php合并图片
链接分享:http://ybzwz.com/article/ddjepij.html