PHP FOREACH 循环出来的数组,我现在想进行合并 并且相加。 代码如下
$new_arr[]=array("new_id"=>$arr[$key][$keys],"new_value"=>$newvalue);在foreach内这个是我定义的...
$new_arr[]=array("new_id"=> $arr[$key][$keys],"new_value"=>$newvalue);
在foreach内这个是我定义的数组,
我在循环外,
var_dump($new_arr); 后 所出现的结果是,
array(4) {
[0]=> array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> float(0.5) }
[1]=> array(2) { ["new_id"]=> string(1) "2" ["new_value"]=> float(0.5) }
[2]=> array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> int(1) }
[3]=> array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> float(0.5) }
[4]=> array(2) { ["new_id"]=> string(1) "6" ["new_value"]=> float(0.5) }
}
我现在想实现的最终结果是这样的:
array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> int(2) }
array(2) { ["new_id"]=> string(1) "2" ["new_value"]=> float(0.5)}
array(2) { ["new_id"]=> string(1) "6" ["new_value"]=> float(0.5)}
// 解释一下: 就是new_id 这一列 把所有 相同的值 合并到一起, 并且 对应的
new_value 这一列进行相加。
希望大家能帮我解答, 今天晚上我会一直在线等。 很急, 谢谢大家了。 展开
在foreach内这个是我定义的数组,
我在循环外,
var_dump($new_arr); 后 所出现的结果是,
array(4) {
[0]=> array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> float(0.5) }
[1]=> array(2) { ["new_id"]=> string(1) "2" ["new_value"]=> float(0.5) }
[2]=> array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> int(1) }
[3]=> array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> float(0.5) }
[4]=> array(2) { ["new_id"]=> string(1) "6" ["new_value"]=> float(0.5) }
}
我现在想实现的最终结果是这样的:
array(2) { ["new_id"]=> string(1) "1" ["new_value"]=> int(2) }
array(2) { ["new_id"]=> string(1) "2" ["new_value"]=> float(0.5)}
array(2) { ["new_id"]=> string(1) "6" ["new_value"]=> float(0.5)}
// 解释一下: 就是new_id 这一列 把所有 相同的值 合并到一起, 并且 对应的
new_value 这一列进行相加。
希望大家能帮我解答, 今天晚上我会一直在线等。 很急, 谢谢大家了。 展开
展开全部
$result = array();
foreach($new_arr as $k => $array){
if(isset($result[$array['new_id']]['new_value'])){
$result[$array['new_id']]['new_value'] += $array['new_value'];
}else{
$result[$array['new_id']]['new_value'] = $array['new_value'];
}
}
我测试过,没问题,如果还有什么不对的随时留言。
foreach($new_arr as $k => $array){
if(isset($result[$array['new_id']]['new_value'])){
$result[$array['new_id']]['new_value'] += $array['new_value'];
}else{
$result[$array['new_id']]['new_value'] = $array['new_value'];
}
}
我测试过,没问题,如果还有什么不对的随时留言。
展开全部
没看太明白,以前我写过一个.
你觉得好的话,就拿去借鉴一下.
Dim b(0 To 6) As String
Dim a(0 To 6) As String
Dim z(0 To 6) As String
Dim c As String
Private Sub Command1_Click()
If Command1.Caption = "开始" Then
Command1.Caption = "停止"
Timer1.Enabled = True
Timer1.Interval = 10
Else
Command1.Caption = "开始"
Timer1.Enabled = False
Call aaa
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
For I = 0 To 6
Text1(I) = ""
Text1(I).Font = "宋体"
Text1(I).FontSize = 20
Text1(I).Alignment = 2
Next I
End Sub
Private Sub Timer1_Timer()
'生成7位随机不重复的数字
For j = 0 To 6
1
Randomize
c = Int(35 * Rnd 1)
Text1(j) = c
If j > 0 Then
For e = 0 To j - 1
If Text1(j) = Text1(e) Then
GoTo 1
End If
Next e
End If
Next j
End Sub
Public Sub aaa()
'按从小到大顺序排序
For Y = 0 To 6
For I = Y To 6
If Val(Text1(Y)) > Val(Text1(I)) Then
TEMP = Text1(Y)
Text1(Y) = Text1(I)
Text1(I) = TEMP
End If
Next I
Next Y
End Sub
你觉得好的话,就拿去借鉴一下.
Dim b(0 To 6) As String
Dim a(0 To 6) As String
Dim z(0 To 6) As String
Dim c As String
Private Sub Command1_Click()
If Command1.Caption = "开始" Then
Command1.Caption = "停止"
Timer1.Enabled = True
Timer1.Interval = 10
Else
Command1.Caption = "开始"
Timer1.Enabled = False
Call aaa
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
For I = 0 To 6
Text1(I) = ""
Text1(I).Font = "宋体"
Text1(I).FontSize = 20
Text1(I).Alignment = 2
Next I
End Sub
Private Sub Timer1_Timer()
'生成7位随机不重复的数字
For j = 0 To 6
1
Randomize
c = Int(35 * Rnd 1)
Text1(j) = c
If j > 0 Then
For e = 0 To j - 1
If Text1(j) = Text1(e) Then
GoTo 1
End If
Next e
End If
Next j
End Sub
Public Sub aaa()
'按从小到大顺序排序
For Y = 0 To 6
For I = Y To 6
If Val(Text1(Y)) > Val(Text1(I)) Then
TEMP = Text1(Y)
Text1(Y) = Text1(I)
Text1(I) = TEMP
End If
Next I
Next Y
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询