PHP数组:for中的两次if语句
我很难两次完成if语句。。其余的工作正常,但当我尝试签入另一个子阵列时,它不工作 编辑:PHP数组:for中的两次if语句,php,arrays,multidimensional-array,oscommerce,Php,Arrays,Multidimensional Array,Oscommerce,我很难两次完成if语句。。其余的工作正常,但当我尝试签入另一个子阵列时,它不工作 编辑: -第一个if条件是检查产品类型 -第二个if条件是检查大小属性。 -产品:运动衫和运动衫 -尺码:4号、6号、8号、11号、12号和14号 需要禁用($this->enabled=false;)除14英寸以外的那些尺寸 这是我从$order->products[$I] Array ( [qty] => 1 [name] => .Mr Mickey T-shirt [mod
-第一个if条件是检查产品类型 -第二个if条件是检查大小属性。 -产品:运动衫和运动衫 -尺码:4号、6号、8号、11号、12号和14号 需要禁用($this->enabled=false;)除14英寸以外的那些尺寸 这是我从$order->products[$I]
Array
(
[qty] => 1
[name] => .Mr Mickey T-shirt
[model] =>
[image] => blusa-mr-mrs-blusao-sweat-camisolas-portugal.png
[tax] => 20
[tax_description] => IVA 23%
[price] => 10.8333
[final_price] => 16.6666
[weight] => 0.00
[id] => 1342{18}135{17}132{19}148
[attributes] => Array
(
[0] => Array
(
[option] => s_cor_produto
[value] => branco
[option_id] => 18
[value_id] => 135
[prefix] => +
[price] => 0.0000
)
[1] => Array
(
[option] => s_produto
[value] => sweatshirt_crianca
[option_id] => 17
[value_id] => 132
[prefix] => +
[price] => 5.8333
)
[2] => Array
(
[option] => s_tamanho_produto
[value] => 8_anos
[option_id] => 19
[value_id] => 148
[prefix] => +
[price] => 0.0000
)
)
)
Array
(
[qty] => 1
[name] => Adivinha quem vai ser mama shirt
[model] =>
[image] => adivinha-quem-vai-ser-mama-tshirt-gravida.png
[tax] => 20
[tax_description] => IVA 23%
[price] => 10.8333
[final_price] => 10.8333
[weight] => 0.00
[id] => 1860{20}157{18}139{17}128{19}152
[attributes] => Array
(
[0] => Array
(
[option] => s_cor_impressao
[value] => branco
[option_id] => 20
[value_id] => 157
[prefix] => +
[price] => 0.0000
)
[1] => Array
(
[option] => s_cor_produto
[value] => azul_royal
[option_id] => 18
[value_id] => 139
[prefix] => +
[price] => 0.0000
)
[2] => Array
(
[option] => s_produto
[value] => tshirt_mulher
[option_id] => 17
[value_id] => 128
[prefix] => +
[price] => 0.0000
)
[3] => Array
(
[option] => s_tamanho_produto
[value] => M
[option_id] => 19
[value_id] => 152
[prefix] => +
[price] => 0.0000
)
)
)
有时它可以包含1或4个子阵列。这是osCommerce的cod付款,我试图根据它的属性从这个方法中排除特定的产品。
这在第一个if语句中可以很好地检查[value],但是注释了语法的行是正确的,但不起作用。这是我得到的代码:
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
if ($order->products[$i]['weight'] > '0.00') {
$this->enabled = false;
}
if ( (isset($order->products[$i]['attributes'])) && (sizeof($order->products[$i]['attributes']) > 0) ) {
for ($j=0, $n2=sizeof($order->products[$i]['attributes']); $j<$n2; $j++) {
if ( ($order->products[$i]['attributes'][$j]['option'] == 's_produto') && ($order->products[$i]['attributes'][$j]['value'] == 'sweatshirt_crianca') ) {
//if ( ($order->products[$i]['attributes'][$j]['option'] == 's_tamanho_produto') && ($order->products[$i]['attributes'][$j]['value'] == '14_anos') ) {
$this->enabled = false;
//}
}
else if ( ($order->products[$i]['attributes'][$j]['option'] == 's_produto') && ($order->products[$i]['attributes'][$j]['value'] == 'sweatshirtc_crianca_capuz') ) {
//if ( ($order->products[$i]['attributes'][$j]['option'] == 's_tamanho_produto') && ($order->products[$i]['attributes'][$j]['value'] != '14_anos') ) {
$this->enabled = false;
//}
}
}
}
}
for($i=0,$n=sizeof($order->products);$i产品[$i]['weight']>'0.00'){
$this->enabled=false;
}
如果((isset($order->products[$i]['attributes'])和&(sizeof($order->products[$i]['attributes'])>0)){
对于($j=0,$n2=sizeof($order->products[$i]['attributes'];$jproducts[$i]['attributes'][$j]['option']=='s\u produto')和($order->products[$i]['attributes'][$j]['value']=='s crianca')){
//如果($order->products[$i]['attributes'][$j]['option']=='s_tamanho_produto')&($order->products[$i]['attributes'][$j]['value']='14_anos')){
$this->enabled=false;
//}
}
如果($order->products[$i]['attributes'][$j]['option']='s_produto')&($order->products[$i]['attributes'][$j]['value']='sweatshirtc\u crianca\u capuz')){
//如果($order->products[$i]['attributes'][$j]['option']='s_tamanho_produto')&($order->products[$i]['attributes'][$j]['value']!='14_anos')){
$this->enabled=false;
//}
}
}
}
}
有什么解决办法吗?谢谢。西迪尔抢在我前面,但我的思路与此相似:
// loop through each product
foreach ( $order->products as $prod )
{
if($prod['weight'] > '0.00')
{
$this->enabled = false;
}
if( isset($prod['attributes']) && is_array($prod['attributes']) )
{
$matching_product_type_found = false;
$matching_valid_size_found = false;
foreach ( $prod['attributes'] as $attr )
{
if ( $attr['option'] == 's_produto' &&
( $attr['value'] == 'sweatshirt_crianca' ||
$attr['value'] == 'sweatshirtc_crianca_capuz'
) )
{
$matching_product_type_found = true;
}
if ( $attr['option'] == 's_tamanho_produto' && $attr['value'] == '14_anos' )
{
$matching_valid_size_found = true;
}
}
if( $matching_product_type_found == true )
{
$this->enabled = $matching_valid_size_found;
}
}
}
我将使用
foreach
重写您的循环,使其更具可读性,并且更容易出错。此外,还要注意比较(=
与==
)。试着这样做:
foreach ($order->products as $product) {
if ($product['weight'] > 0.00) {
$this->enabled = false;
}
if (!empty($product['attributes'])) {
foreach ($product['attributes'] as $attribute) {
if ($attribute['option'] === 's_produto') {
switch ($attribute['value']) {
case "sweatshirt_crianca" :
// .. do some code
break;
case "s_tamanho_produto" :
// .. do some code
break;
case "14_anos" :
// .. do some code
break;
case "something else" :
// .. do some code
break;
default:
break;
}
}
}
}
}
代码的主要问题是您想要测试不同的 数组元素,但您在逐个迭代时执行此操作。代码 在
for
循环中,只能看到
迭代。因此,当嵌套if
语句时,基本上
测试电流元素是否等于某个值,然后测试
你要测试这个元素是否等于其他元素。这
逻辑将失败
由于显然只有选项
和值
键很重要,因此
建议使用第一个数组创建另一个数组。然后,您可以访问
一次完成所有这些属性,而不仅仅是当前属性
在迭代器中。例如,下面是一个简化的属性数组
关于问题的最后一个例子:
$attributes = [
s_cor_impressao => 'branco'
s_cor_produto => 'azul_royal'
s_produto => 'tshirt_mulher'
s_tamanho_produto => 'M'
];
为了方便地创建它并简化代码,我建议使用
循环。此外,您不需要检查
数组中有个项目。如果没有项目,则不会显示foreach
被处决了。值得检查的是,它是否已设置,如您所做的,如果您
事先不知道。但另一种方法是直接跳到
下一次迭代(如果未使用控件设置)
结构
foreach ($order->products as $product) {
if ($product['weight'] > 0)
$this->enabled = false;
if (!isset($product['attributes']) || !is_array($product['attributes']))
continue;
$attributes = [];
foreach ($products['attributes'] as $attr)
$attributes[ $attr['option'] ] = $attr['value'];
# [tests]
}
现在,在上面的测试标记中,您可以进行如下测试:
if ($attr['s_produto'] == 'sweatshirt_crianca' &&
$attr['s_tamanho_produto'] == '14_anos') {
# do something
# $this->enabled = false;
} else if (...) {
# do something else
}
如果s应该在其他的内部,那么这些被注释的
s是否应该在其他的内部?因为您正在测试A=B。如果是,那么您将测试A=C。当然不是。好吧,这些属性似乎都不会同时通过这两个条件(一旦未注释)。你确定它们应该像你一样嵌套吗?你的逻辑没有意义。也许用伪代码给我们你的逻辑,这样我们就知道你想要完成什么。第一个条件是检查2种特定类型的产品,第二个条件是检查我得到的尺寸:4,6,8,11,12,14。只允许'14_anos'。sidyll首先检查A=B是否有效,然后我需要检查C=D(对应产品大小的属性)是否有效,但它不起作用。我无法使它起作用。。我需要切换到另一个子阵列,我得到了不同的参数,我必须在[option]=>s_produto到[option]=>s_tamanho_produto之间切换,检查它是否[value]=>14_anoss_tamanho_produto属于$order->products[$I]['attributes'[$j]['option']