猿问

将数组与有组织的有效载荷数据结合

我正在尝试从这个 for 循环中获取唯一值并组织它们 Payload:


$name = ['Mazda','Mazda','Lexus', 'Lexus'];

$count = [1,1,1,1];

$gp =[54,35,23,46];

我有一个有效载荷,我试图根据“唯一”名称将数据组织到数组中,这样我就可以汇总结果并最终将其传递给 PDF。我要创建的列是总列。


例子。


NAME  | C | GP |

------|---|----| 

Mazda | 2 | 89 |

Lexus | 2 | 69 |

最好的方法是什么?我想过做 array_unique


九州编程
浏览 101回答 1
1回答

胡子哥哥

这是我的方法// Data$name = ['Mazda','Mazda','Lexus', 'Lexus'];$count = [1,1,1,1];$gp =[54,35,23,46];// declare result array$result = [];// do Loopingfor($i = 0; $i < count($name); $i++) {&nbsp; &nbsp; $result[ $name[$i] ] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "C" => ($result[ $name[$i] ]['C'] ?? 0) + $count[$i],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "GP" => ($result[ $name[$i] ]['GP'] ?? 0) + $gp[$i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];}print_r($result);演示: https:&nbsp;//3v4l.org/U3nQ0
随时随地看视频慕课网APP
我要回答