如何将整数作为值从数组传递到PHP中的IF语句?

时间:2014-04-11 10:30:55

标签: php

接触以下内容的最佳方法是什么:

我试图将数组中的值作为变量传递给IF语句。我希望语句遍历数组中的每个值(在本例中是一系列整数)并执行IF语句。到目前为止,这是我的代码:

<?php

$con_size = array (35,355,36,37,375,38,385,39,395,40,405,41,415,42,425,43,435,44,445,45,455,46,465,47,475,48,485);
$arrlength=count($con_size);

for($x=0;$x<$arrlength;$x++) {
// check if size is available 
if($line['quantity_c_size_'.$con_size.'_chain'] > 0 ) { 
    ?>

    <?=$line['product_id']?>, 
    <?=$line['code_c_size_'.$con_size.'']?>,

    <?=$line['title']?>, 

    <?=preg_replace('/[^\da-z]/i', ' ', $line['amazon_desc']) ?>,
    <?=$size?>,
    <?=$line['colour']?>,
    <?=$line['material']?>,
    <?=$line['upper']?>,
    <?=$line['lining']?>,
    <?=$line['sole']?>,
    <?=$line['heel']?>,
    <?=$line['material2']?>,

    Shoes, 
    UPDATE, 
    <?=$line['shoe_id']?>, 
    http://www.getashoe.co.uk/full/<?=$line['product_id']?>_1.jpg, http://www.getashoe.co.uk/full/<?=$line['product_id']?>_2.jpg,
    http://www.getashoe.co.uk/full/<?=$line['product_id']?>_3.jpg, http://www.getashoe.co.uk/full/<?=$line['product_id']?>_4.jpg,
    <?=$line['price']?>,
    <?=$line['price']?>,
    GBP,
    <?=$line['quantity_c_size_'.$con_size.'_chain']?>,
    A_GEN_NOTAX,
    <?=$line['added_y']?>-<?=$line['added_m']?>-<?=$line['added_d']?>,
    <?=$line['added_y']?>-<?=$line['added_m']?>-<?=$line['added_d']?>,
    ,
    ,
    ,
    ,
    1,
    ,
    ,
    ,
    ,
    2,
    1

    <br /><br />
    <?
    // finish checking if size is available
    } }

    ?>

所以我想要数组中的值,即&#39; 35&#39;传入IF语句代替$ con_size,以便该行变为&#39; quantity_c_size_35_chain&#39;。

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

您需要循环中的数组键来获取循环中当前位置的值。

$con_size[$x]

所以你有条件会看起来像这样

if ($line['quantity_c_size_'.$con_size[$x].'_chain'] > 0) {

答案 1 :(得分:0)

您可以使用以下内容:

foreach ($con_size as $value){
  if($line['quantity_c_size_'.$value.'_chain'] > 0 ){
    //Code
  }
}

答案 2 :(得分:0)

您还可以执行以下操作

<?php

$con_size = array (35,355,36,37,375,38,385,39,395,40,405,41,415,42,425,43,435,44,445,45,455,46,465,47,475,48,485);

foreach ($con_size as $value) {

// check if size is available 
if($line['quantity_c_size_'.$value.'_chain'] > 0 ) { 
    ?>

    <?=$line['product_id']?>, 
    <?=$line['code_c_size_'.$value.'']?>,

    <?=$line['title']?>, 

    <?=preg_replace('/[^\da-z]/i', ' ', $line['amazon_desc']) ?>,
    <?=$size?>,
    <?=$line['colour']?>,
    <?=$line['material']?>,
    <?=$line['upper']?>,
    <?=$line['lining']?>,
    <?=$line['sole']?>,
    <?=$line['heel']?>,
    <?=$line['material2']?>,

    Shoes, 
    UPDATE, 
    <?=$line['shoe_id']?>, 
    http://www.getashoe.co.uk/full/<?=$line['product_id']?>_1.jpg, http://www.getashoe.co.uk/full/<?=$line['product_id']?>_2.jpg,
    http://www.getashoe.co.uk/full/<?=$line['product_id']?>_3.jpg, http://www.getashoe.co.uk/full/<?=$line['product_id']?>_4.jpg,
    <?=$line['price']?>,
    <?=$line['price']?>,
    GBP,
    <?=$line['quantity_c_size_'.$value.'_chain']?>,
    A_GEN_NOTAX,
    <?=$line['added_y']?>-<?=$line['added_m']?>-<?=$line['added_d']?>,
    <?=$line['added_y']?>-<?=$line['added_m']?>-<?=$line['added_d']?>,
    ,
    ,
    ,
    ,
    1,
    ,
    ,
    ,
    ,
    2,
    1

    <br /><br />
    <?
    // finish checking if size is available
    } }

    ?>

答案 3 :(得分:0)

您可以使用foreach()代替for(),

foreach($con_size as $val){
  echo $val;
}

$ val返回数组值。