How to pass intergers as values from an array into an IF statement in PHP?

866

What would be the best way to approach the following:

I'm trying to pass values from an array into an IF statement as variables. I would like the statement to loop through every value (in this case a series of integers) in the array and carry out the IF statement. Here's my code so far:

<?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
    } }

    ?>

So I would like the value from the array i.e. '35' passing into the IF statement in place of $con_size so that the line will become 'quantity_c_size_35_chain'.

Any help would be appreciated.

902

Answer

Solution:

You need the array key from your loop to get the value of the current position in your loop.

$con_size[$x]

So you conditional will look like this

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

Answer

Solution:

You can use something like the following:

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

Answer

Solution:

You can also do the following

<?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
    } }

    ?>
632

Answer

Solution:

You can use foreach() instead of for() like,

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

$val returns the array values.

People are also looking for solutions to the problem: php - How to use laravel migrations

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.