1、這里假設(shè)數(shù)組為data,可以根據(jù)自己需要更改第一行代碼即可。
成都創(chuàng)新互聯(lián)專注于象州企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都做商城網(wǎng)站。象州網(wǎng)站建設(shè)公司,為象州等地區(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
data可以是行向量、列向量、矩陣都行。
2、輸出一個n*2的矩陣,第一列是元素,第二列是元素出現(xiàn)的次數(shù)。
3、代碼如下:
data=[1
2
3
1
3
2
3
3
2
4];
data=data(:);
uniquedata=unique(data);
out=zeros(length(uniquedata),2);
for
ni=1:length(uniquedata)
out(ni,1)=uniquedata(ni);
out(ni,2)=sum(data==uniquedata(ni));
end
disp(out)
結(jié)果為:
1
2
2
3
3
4
4
1
即1出現(xiàn)2次,2出現(xiàn)3次。。。
覺得有幫助就采納吧。
array_unique函數(shù)提供了過濾重復(fù)的值的功能,但是該函數(shù)只能處理一維數(shù)組,如果遇到多維數(shù)組我們可以現(xiàn)將其轉(zhuǎn)化為一維數(shù)組。示例代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* $data 是你要處理數(shù)據(jù)
* $result 是輸出的結(jié)果
*/
//將其轉(zhuǎn)化為一維數(shù)組
foreach ($data as $row) {
$row = serialize($row);
}
unset($row);
$data = array_unique($data);
//還原多維數(shù)組
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";
//輸出結(jié)果
print_r($result);
?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);
?