Reputation: 137
I am trying to compare two prices within an associative array. I need to pull both prices out so that I can make a calculation. The array I have is:
[10] => Array
(
[A Is For Annabelle 681 2 (fat Quarter)] => 2.8500
[A Is For Annabelle 681 2 (yardage)] => 7.9500
)
And the code i'm trying is:
$fqPrice;
$ydPrice;
foreach ($value as $key => $value) {
if (strpos($key, 'yd') !== false || strpos($key, 'yardage') !== false ) {
$ydPrice = $value;
}
if (strpos($key, 'fq') !== false || strpos($key, 'fat quarter') !== false ) {
$fqPrice = $value;
}
}
It gets to the first if statement but doesn't execute the second one.
Upvotes: 0
Views: 51
Reputation:
adding the extra array dimension loop (you probably already have this), i changed to a preg_match
which is 'cleaner':
<?php
$value = array(10=>array('A Is For Annabelle 681 2 (fat Quarter)'=>'2.8500','A Is For Annabelle 681 2 (yardage)'=>'7.9500'));
$ydPrice=$fqPrice='';
foreach ($value as $first){
foreach ($first as $key=>$value){
if (preg_match('#yd|yardage#',$key)){
$ydPrice = $value;
}
if (preg_match('#fq|fat Quarter#',$key)){
$fqPrice = $value;
}
}
}
echo 'y=' . $ydPrice;
echo 'f=' . $fqPrice;
?>
demo: http://ideone.com/JTJNWx
if your only interest in $value[10]
, you can just use foreach($value[10] as $key=>$value){}
Upvotes: 1
Reputation: 1506
well, your second one IS being executed, but it returns false
on both expressions; so it means the $value
will not be assigned to $fqPrice
.
You need to remember that strpos()
is case-sensitive, you can either change to use strpos($key, 'fat Quarter')
or use the stripos
function instead.
Upvotes: 1