iT邦幫忙

0

PHP array 陣列加總的邏輯問題

  • 分享至 

  • xImage

假設我有一組陣列

$array = (
[0] =>  ( [user_id] => 100440 [len_time] => 113    [create_date] => 20190801 ) 
[1] =>  ( [user_id] => 100440 [len_time] => 55     [create_date] => 20190801 ) 
[2] =>  ( [user_id] => 100440 [len_time] => 232    [create_date] => 20190801 ) 
[3] =>  ( [user_id] => 100911 [len_time] => 299    [create_date] => 20190814 ) 
[4] =>  ( [user_id] => 101073 [len_time] => 679    [create_date] => 20190808 ) 
[5] =>  ( [user_id] => 101088 [len_time] => 3436   [create_date] => 20190806 )
[6] =>  ( [user_id] => 101088 [len_time] => 101073 [create_date] => 20190806 ) 
[7] =>  ( [user_id] => 101088 [len_time] => 2755   [create_date] => 20190812 ) 
[8] =>  ( [user_id] => 101088 [len_time] => 75     [create_date] => 20190814 ) 
[9] =>  ( [user_id] => 101153 [len_time] => 294    [create_date] => 20190801 ) 
)

我想要判斷
每個 key 'user_id', 'create_date'與
其他的 key 'user_id' , 'create_date' 相等的話
就將len_time相加
最後我想要的結果是

new_array = (
[0] =>  ( [user_id] => 100440 [len_time] => 400    [create_date] => 20190801 ) 
[1] =>  ( [user_id] => 100911 [len_time] => 299    [create_date] => 20190814 ) 
[2] =>  ( [user_id] => 101073 [len_time] => 679    [create_date] => 20190808 ) 
[3] =>  ( [user_id] => 101088 [len_time] => 104509 [create_date] => 20190806 )
[4] =>  ( [user_id] => 101088 [len_time] => 2755   [create_date] => 20190812 ) 
[5] =>  ( [user_id] => 101088 [len_time] => 75     [create_date] => 20190814 ) 
[6] =>  ( [user_id] => 101153 [len_time] => 294    [create_date] => 20190801 ) 
)

不知道有沒有辦法做到
我卡了有點久所以跑來發問 QQ!!

看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2019-08-28 15:43:44 檢舉
你原本的寫法是什麼呢
moonfish iT邦新手 4 級 ‧ 2019-08-28 16:51:04 檢舉
$new_array = array();

foreach($array as $k=>$v) {
$vFlag = $v['user_id'].$v['create_date'];

if(isset($new_array[$vFlag])) {
$new_array[$vFlag]['len_time'] += $v['len_time'];
} else {
$new_array[$vFlag] = $v;
}
}

print_r(array_values($new_array));
結果用樓上的就解出來了QQ!!回想一下這是很基礎的功夫 感謝你
甲土豆 iT邦新手 5 級 ‧ 2019-08-29 13:39:03 檢舉
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
小魚
iT邦大師 1 級 ‧ 2019-08-28 15:42:58

所以你現在是怎麼做的呢?

這篇文章有一個留言解決掉了 XD

0
ccutmis
iT邦高手 2 級 ‧ 2019-08-28 17:22:37
<?php
$array = [
[100440,113,20190801], [100440,55,20190801], [100440,232,20190801],
[100911,299,20190814], [101073,679,20190808], [101088,3436,20190806],
[101088,101073,20190806], [101088,2755,20190812], [101088,75,20190814],
[101153,294,20190801]
];

$array_new=[];

for($i=0;$i<count($array);$i++){
	if(count($array_new)==0){
		array_push($array_new,[$array[$i][0],$array[$i][1],$array[$i][2]]);
	}else{
		$ck=-1;
		for($j=0;$j<count($array_new);$j++){
			if($array[$i][0]==$array_new[$j][0]&&
            $array[$i][2]==$array_new[$j][2]){
				$ck=$j;
				$array_new[$j][1]+=$array[$i][1];
				break;
			}
		}
		if($ck==-1){
			array_push($array_new,[$array[$i][0],$array[$i][1],$array[$i][2]]);
		}
	}
}

echo '$array 陣列長度: '.count($array).'<hr/>';
echo '$array_new 陣列長度: '.count($array_new).'<hr/>';
var_dump($array);
echo '<hr/>';
var_dump($array_new);
?>

參考看看...

0
犬千賀
iT邦新手 3 級 ‧ 2019-09-11 12:00:47

練練手 =DD

$array = array(...ry);

$_arr = array();
foreach( $array as $d ){
	$_arr[ $d['user_id'].'_'.$d['create_date'] ]+=$d['len_time'];
}

$new_arr = array();
foreach( $_arr as $key => $len_time ){
	list( $user_id, $create_date ) = explode('_', $key);
	$new_arr[] = compact('user_id', 'len_time', 'create_date');
}

echo "<pre>",print_r($new_arr),"</pre>";

我要發表回答

立即登入回答