帮我把数组值组合起来。 我有相同的数组: 在我的情况下,我应该像钥匙一样保存客户名称。
Array
(
[Test Name] => Array
(
[0] => Array
(
[name] =>
banana
[id] =>
45002
[quantity] =>
10
)
[1] => Array
(
[name] =>
banana
[id] =>
45002
[quantity] =>
20
)
[3] => Array
(
[name] =>
apple
[id] =>
23402
[qua] =>
1
)
[5] => Array
(
[name] =>
cherry
[id] =>
40017
[qua] =>
7
)
如何得到这样的东西:
Array
(
[Test Name] => Array
(
[0] => Array
(
[name] =>
banana
[id] =>
45002
[quantity] =>
30 // summ quantity but unique name and id
)
[1] => Array
(
[name] =>
apple
[id] =>
23402
[qua] =>
1
)
[2] => Array
(
[name] =>
cherry
[id] =>
40017
[qua] =>
7
)
在我的情况下,我应该像钥匙一样保存客户名称。 然后我会把它上传到桌子上。
答案 0 :(得分:0)
您可以使用import matplotlib.pyplot as plt
import numpy as np
from scipy import fft, arange
csv = np.genfromtxt ('/Users/shaunbarney/Desktop/Results/quadOscillations.csv', delimiter=",",dtype=float)
x = csv[:,0]
y = csv[:,1]
x = x - 6318 #Remove start offset
av=0
for i in xrange(1097): #Calculate average sampling time in seconds oscillations
if i == 1076:
avSampleTime = av/1097000 #
break
av = av + (x[i+1]-x[i])
Fs = 1/avSampleTime #Average sampling freq.
n = 1079 #no.Samples
k = arange(n)
Ts = n/Fs
frq = k/Ts #Frequency range two sided
frq = frq[range(n/2)] #Frequency range one sided
Y = fft(y)/n #Fast fourier transfors
Y = Y[range(n/2)] #Normalise
# PLOTS
plt.subplot(2,1,1)
plt.plot(frq,abs(Y),'r') # plotting the spectrum
plt.xlabel('Freq (Hz)')
plt.ylabel('|Y(freq)|')
plt.grid('on')
plt.subplot(2,1,2)
plt.plot(x,y)
plt.xlabel('Time (ms)')
plt.ylabel('Angle (degrees)')
plt.grid('on')
plt.show()
和array_map
来完成它:
array_reduce
来自文档:
array_map() 在将回调函数应用于每个元素后返回包含array1的所有元素的数组。
array_reduce() 将回调函数迭代地应用于数组元素,以便将数组减少为单个值。
在我们的例子中,给$results = array_map(function ($result) {
return array_reduce($result, function ($carry, $item) {
if (isset($carry[$item['id']])) {
$carry[$item['id']]['quantity'] += $item['quantity'];
} else {
$carry[$item['id']] = $item;
}
return $carry;
}, array());
}, $results);
函数的回调返回一个最终数组,其中包含array_reduce
的唯一项。