将 php 数组内爆为格式化文本
我有这个数组:
array(122) {
["1AB168820010"]=> array(3) {
["MACHINE_NAME"]=> "L1XP2A"
["FEEDER_SLOT"]=> "114"
["REJECT_RATE"]=> float(0.0394)
["DEFECT_QTY"]=> int(2)
["SOLDER BALL"]=> int(2)
}
["1AB037870031"]=> array(5) {
["MACHINE_NAME"]=> "L2CP7A"
["FEEDER_SLOT"]=> "155"
["REJECT_RATE"]=> float(2.3022)
["DEFECT_QTY"]=> int(39)
["COMPONENT TOMBSTONED"]=> int(31)
["SOLDER BALL"]=> int(2)
["COMPONENT BILLBOARD"]=> int(6)
}
["1AB144890021"]=> array(7) {
["MACHINE_NAME"]=> "L21P3A"
["FEEDER_SLOT"]=> "214"
["REJECT_RATE"]=> float(0.0225)
["DEFECT_QTY"]=> int(8)
["SOLDER INSUFFICIENT "]=> int(2)
["SOLDER BAD"]=> int(2)
["SOLDER BALL"]=> int(2)
["COMPONENT MISSING"]=> int(1)
["COMPONENT BILLBOARD"]=> int(1)
}
["1AB144890033"]=> array(7) {
["MACHINE_NAME"]=> "L1CP7A"
["FEEDER_SLOT"]=> "234"
["REJECT_RATE"]=> float(0.0142)
["DEFECT_QTY"]=> int(7)
["SOLDER INSUFFICIENT "]=> int(1)
["SOLDER BAD"]=> int(1)
["COMPONENT MISSING"]=> int(3)
["COMPONENT SKEW"]=> int(1)
["COMPONENT TOMBSTONED"]=> int(1)
}
#...more
}
我需要循环遍历该数组并从数组输出创建一个看起来像这样的字符串,但不知道最好的方法...请帮助
1AB168820010 ( 0.0394% ) #<-this is the 'REJECT_RATE'
-Machine: L1XP2A
-Feeder: 114
SOLDER BALL ( 100% ) #<-'SOLDER BALL' value (2) divided by 'DEFECT_QTY' (2) * 100
------------------------
1AB037870031 ( 2.3022% )
-Machine: L2CP7A
-Feeder: 155
COMPONENT TOMBSTONED ( 79.48% ) #<- ( 31 / 39 ) * 100
COMPONENT BILLBOARD ( 15.38% ) #<- ( 6 / 39 ) * 100
SOLDER BALL ( 5.12% ) #<- ( 2 / 39 ) * 100
------------------------
1AB144890021 ( 0.0225% )
-Machine: L2IP3A
-Feeder: 214
SOLDER INSUFFICIENT ( 25% )
SOLDER BAD ( 25% )
SOLDER BALL ( 25% )
COMPONENT MISSING ( 12.5% )
COMPONENT BILLBOARD ( 12.5% )
------------------------
1AB144890033 ( 0.0142% )
-Machine: L1CP7A
-Feeder: 234
SOLDER INSUFFICIENT ( 14.3% )
SOLDER BAD ( 14.3% )
COMPONENT MISSING ( 42.8% )
COMPONENT SKEWED ( 14.3% )
COMPONENT TOMBSTONED ( 14.3% )
我的主要问题是我不确定如何处理我不知道每个零件号的缺陷数量(即组件缺失、组件倾斜、焊接不良)、有多少缺陷(以及它有哪些缺陷)会有所不同,因此我不能只硬编码“组件缺失:” [一些计算]'进入我的foreach循环......
I have this array:
array(122) {
["1AB168820010"]=> array(3) {
["MACHINE_NAME"]=> "L1XP2A"
["FEEDER_SLOT"]=> "114"
["REJECT_RATE"]=> float(0.0394)
["DEFECT_QTY"]=> int(2)
["SOLDER BALL"]=> int(2)
}
["1AB037870031"]=> array(5) {
["MACHINE_NAME"]=> "L2CP7A"
["FEEDER_SLOT"]=> "155"
["REJECT_RATE"]=> float(2.3022)
["DEFECT_QTY"]=> int(39)
["COMPONENT TOMBSTONED"]=> int(31)
["SOLDER BALL"]=> int(2)
["COMPONENT BILLBOARD"]=> int(6)
}
["1AB144890021"]=> array(7) {
["MACHINE_NAME"]=> "L21P3A"
["FEEDER_SLOT"]=> "214"
["REJECT_RATE"]=> float(0.0225)
["DEFECT_QTY"]=> int(8)
["SOLDER INSUFFICIENT "]=> int(2)
["SOLDER BAD"]=> int(2)
["SOLDER BALL"]=> int(2)
["COMPONENT MISSING"]=> int(1)
["COMPONENT BILLBOARD"]=> int(1)
}
["1AB144890033"]=> array(7) {
["MACHINE_NAME"]=> "L1CP7A"
["FEEDER_SLOT"]=> "234"
["REJECT_RATE"]=> float(0.0142)
["DEFECT_QTY"]=> int(7)
["SOLDER INSUFFICIENT "]=> int(1)
["SOLDER BAD"]=> int(1)
["COMPONENT MISSING"]=> int(3)
["COMPONENT SKEW"]=> int(1)
["COMPONENT TOMBSTONED"]=> int(1)
}
#...more
}
I need to loop through the array and create a string from the array output that looks like this but don't know the best way...please help
1AB168820010 ( 0.0394% ) #<-this is the 'REJECT_RATE'
-Machine: L1XP2A
-Feeder: 114
SOLDER BALL ( 100% ) #<-'SOLDER BALL' value (2) divided by 'DEFECT_QTY' (2) * 100
------------------------
1AB037870031 ( 2.3022% )
-Machine: L2CP7A
-Feeder: 155
COMPONENT TOMBSTONED ( 79.48% ) #<- ( 31 / 39 ) * 100
COMPONENT BILLBOARD ( 15.38% ) #<- ( 6 / 39 ) * 100
SOLDER BALL ( 5.12% ) #<- ( 2 / 39 ) * 100
------------------------
1AB144890021 ( 0.0225% )
-Machine: L2IP3A
-Feeder: 214
SOLDER INSUFFICIENT ( 25% )
SOLDER BAD ( 25% )
SOLDER BALL ( 25% )
COMPONENT MISSING ( 12.5% )
COMPONENT BILLBOARD ( 12.5% )
------------------------
1AB144890033 ( 0.0142% )
-Machine: L1CP7A
-Feeder: 234
SOLDER INSUFFICIENT ( 14.3% )
SOLDER BAD ( 14.3% )
COMPONENT MISSING ( 42.8% )
COMPONENT SKEWED ( 14.3% )
COMPONENT TOMBSTONED ( 14.3% )
My main issue I'm not sure how to handle is that I don't know the number of defects (i.e. COMPONENT MISSING, COMPONENT SKEWED, SOLDER BAD) per partnumber, how many defects (and what defects it has) will vary, therefore I can't just hard-code 'COMPONENT MISSING: [some calc]' into my foreach loop....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有两种方法:简单和“正确”:)
简单方法:
“正确”方法
有类
Machine
:使用“正确”方法的好处是您可以多次重用您的类,并调整输出所有需要它的地方只需修改单个位置的代码即可。
There are two approaches: simple and 'right' :)
Simple approach:
'Right' approach
Have class
Machine
:The benefit ofusing 'right' approach is that youcan reuse your class multiple times, and adjust output in all placesthat require it by modifying code at single place only.
(您可以选择按键对数组进行排序:
ksort($array);
。)接下来,迭代每个元素并构建字符串:
最后,输出字符串:
回显$输出;
。(Optionally, you can sort the array by its keys:
ksort($array);
.)Next, you iterate over each element and build the string:
And finally, you output the string:
echo $output;
.这可行
this could work
利用
foreach
循环遍历数组中的每个对象并相应地输出它们。应修改下面的示例以适合您的样式,但它应该为您提供入门基础:Utilize a
foreach
loop that loops through each object in the array and outputs them accordingly. The example below should be modified to fit your styling, but it should give you the basis to get started:使用
foreach
迭代这些值并根据需要格式化输出字符串。Use
foreach
to iterate through the values and format the output string as desired.