How to pass intergers as values from an array into an IF statement in PHP?
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.
Answer
Solution:
You need the array key from your loop to get the value of the current position in your loop.
So you conditional will look like this
Answer
Solution:
You can use something like the following:
Answer
Solution:
You can also do the following
Answer
Solution:
You can use foreach() instead of for() like,
$val returns the array values.