我有这个数组:
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% )
我的主要问题我不知道如何处理是因为我不知道每个部件的缺陷数量(即组件缺失,组件磨损,焊料坏),缺陷数量(以及缺陷有多少)变化,因此我不能只是硬编码'COMPONENT MISSING:[some calc]'进入我的foreach循环....
答案 0 :(得分:2)
有两种方法:简单和'正确':)
简单方法:
foreach ($array as $machine_key=>$machine){
$solder_ball = $machine['SOLDER_BALL']/ $machine['DEFECT_QTY']*100;
echo "$machine_key ({$machine['REJECT_RATE']})";
echo " -Machine {$machine['MACHINE_NAME']};
echo " -Feeder {$machine['FEEDER_SLOT']}";
echo " SOLDER INSUFFICIENT ( $solder_ball%)";
//...and so on...//
}
'正确'的方法
上课Machine
:
class Machine{
$protected $name, $feeder, $solder_insufficient; //..all youneed to output here..//
function__constructor(Array $params){
$this->name = $params['MACHINE_NAME'];
$this->solder_insufficient = $machine['SOLDER_BALL']/ $machine['DEFECT_QTY']*100;
//..all other params here...//
}
function output(){
echo "{$this->key} ({$this->reject_rate)";
echo " -Machine {$this->key}";
echo " -Feeder {$this->feeder}";
echo " SOLDER INSUFFICIENT ( {$this->solder_insufficient}%)";
//....and so on ..//
}
}
使用“正确”方法的好处是,您可以多次重复使用您的类,并通过仅在单个位置修改代码来调整所有需要它的位置的输出。
答案 1 :(得分:1)
(可选地,您可以通过其键对数组进行排序:ksort($array);
。)
接下来,迭代每个元素并构建字符串:
$output = '';
foreach ($array as $key => $data) {
$output .= $key . ' ( ' . number_format($data['REJECT_RATE'], 3) . '% )' . "\n";
$output .= ' -Machine: ' . $data['MACHINE_NAME'] . "\n";
$output .= ' -Feeder: ' . $data['FEEDER_SLOT'] . "\n";
$output .= ' SOLDER BALL ( ' . number_format(2 / $data['DEFECT_QTY'] * 100, 0) . '% )'. "\n";
// Add more calculation here…
$output .= "------------------------\n";
}
最后,输出字符串:echo $output;
。
答案 2 :(得分:1)
这可行吗
foreach($arr as $id => $item) {
printf('%s ( %s )
-Machine: %s
-Feeder: %s
',
$id,
$item['REJECT_RATE'],
$item['MACHINE_NAME'],
$item['FEEDER_NAME']);
foreach($item as $key => $val) {
if(!in_array($key, array('REJECT_RATE', 'MACHINE_NAME', 'FEEDER_NAME', 'REJECT_QTY'))) {
printf(" %s ( %s )\n", $key, 100*$val/$item['REJECT_QTY']);
}
}
}
答案 3 :(得分:0)
利用循环遍历数组中每个对象的foreach
循环并相应地输出它们。下面的示例应该修改以适合您的样式,但它应该为您提供入门的基础:
<table>
<thead>
<tr><th>Item</th><th>Value</th></tr>
</thead>
<tbody>
<?php
foreach ($myArray as $k => $v){
echo "<tr><td>$k</td><td>$v</td></tr>";
}
?>
</tbody>
</table>
答案 4 :(得分:0)
使用foreach
迭代值并根据需要格式化输出字符串。
foreach( $your_array as $id => $details ) {
foreach( $details as $key => $value ) {
//format your desired output
}
}