0
使用PHP訂單。我想通過一個數組列表並打印出類似於foreach循環,但僅限於指定數量的數組。PHP運行指定的數組數量
$PRODUCTS = array(
// product abbreviation, product name, unit price
// follow valid name/ID rules for product abbreviation
array('prod1', '20" 4:3 (1600 x 1200)', 150),
array('prod2', '24" 16:9 (1920 x 1200)', 250),
array('prod3', '32" 16:9 (1920 x 1080)', 300),
array('prod4', '40" 16:9 (1920 x 1080)', 450),
array('prod5', '46" LCD 16:9 (1920 x 1080)', 600),
array('prod6', '55" LCD 16:9 (1920 x 1080)', 800),
array('prod7', 'Floor Stand (with rental of monitor only)', 100),
);
foreach($PRODUCTS as $product) {
list($abbr, $name, $price) = $product;
// quantity text input
$qty_el = $frm->addInput('text', $abbr . '_qty', 0,
array('size'=>4, 'class'=>'cur', 'pattern'=>'[0-9]+', 'placeholder'=>0,
'onchange'=>'getProductTotal(this)',
'onclick'=>'checkValue(this)', 'onblur'=>'reCheckValue(this)'));
// total text input
$tot_el = $frm->addInput('text', $abbr . '_tot', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur'));
// price hidden input
$price_el = $frm->addInput('hidden', $abbr . '_price', $price);
$tbl->addRow();
$tbl->addCell($name);
$tbl->addCell('$' . number_format($price, 2) . $price_el, 'cur');
$tbl->addCell($qty_el, 'qty');
$tbl->addCell($tot_el);
}
我該如何修改它只打印,例如,只有前3個數組?我期望做到這一點的原因是有一個描述標題分解訂單中的幾個產品 - 也許有更好的方法來做到這一點?如有必要,我可以提供更多的代碼。
謝謝!
你想停止3次循環的PRODUCTS數組後的循環? – IqbalBary
是的,我真的不能想到一個更好的方法來做到這一點,因爲我已經把PRODUCTS數組中的所有東西都輸入到sum/total函數中,所以我不想製作單獨的數組。但我想在幾個產品之間添加標題。 – sixfiveoh
使用'break'語句來停止循環。檢查更新代碼的答案。 – IqbalBary