我有这个代码:
...............................................
'Detalle' => array()
];
foreach ($cart as $line => $item) {
//check condition
if (strtolower($item['description']) === 'something') {
$cond = true;
} else {
$cond= false;
}
$ab['Detalle'][]=array(
'NmbItem' => $item['name'],
'QtyItem' => (int)$item['quantity'],
'PrcItem' => $item['price']
);
if ($cond){
$array2 = array('IndExe' => 1);
array_merge($ab['Detalle'],$array2);
}
}
$ab['Detalle']
仅当条件为真时,如何将“IndExe”添加到数组?我试过 array_merge、array_merge_recursive 但没有。
IndExe 只能为 1,其他值如 0 或 null 是不可能的。我试过了:
$ab['Detalle'][]=array(
'NmbItem' => $item['name'],
'QtyItem' => (int)$item['quantity'],
'PrcItem' => $item['price']
'IndExe' => ($cond? 1 : 0 )
);
但是当cond = false
那时IndExe = 0
,不是我需要的。仅当cond = true
.
我们来引入一个临时数组:
$tempArray=Array(
'NmbItem' => $item['name'],
'QtyItem' => (int)$item['quantity'],
'PrcItem' => $item['price']
);
if ($cond){
$tempArray['IndExe'] = 1;
}
$ab['Detalle'][] = $tempArray;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句