I have an array within an array and I have a variable whose value I want to set depending on the index of the element of the array.
This is the array:
$data_array = array('1A' => array(3.23, 5.23, 1.23, 8.67, 2.3, 2.1, 1.27, 4.24),
'2A' => array(5.8, 2.23, 5.23, 2.67, 2.3, 2.1, 1.27, 4.24),
'3A' => array(3.23, 5.23, 1.23, 8.67, 2.3, 2.1, 1.27, 4.24),
'4A' => array(3.23, 5.23, 1.23, 8.67, 2.3, 2.1, 1.27, 4.24),
'5A' => array(3.23, 5.23, 1.23, 8.67, 2.3, 2.1, 1.27, 4.24),
'6A' => array(3.23, 5.23, 1.23, 8.67, 2.3, 2.1, 1.27, 4.24));
What I want to do is:
When it's at index 0 of array 1A variable quantity value should be set to 1-50
When it's at index 1 of array 1A, the variable value should be set to quantity = '51-100'
I've coded the foreach:
foreach ( $data_array as $cp => $value ) {
foreach ($value as $price){
if ($value[0]) {
$quantity = '1-59';
}
else if ($value[1]){
$quantity = '51-100';
}
else if ($value[2]){
$quantity = '101-150';
}
else if ($value[3]){
$quantity = '151-200';
}
else if ($value[4]){
$quantity = '201-250';
}
else if ($value[5]){
$quantity = '251-300';
}
else if ($value[6]){
$quantity = '301-350';
}
else if ($value[7]){
$quantity = '351-400';
}
//output
$values[] = $wpdb->prepare( "(%s, $f)", $quantity, $price );
}
}
This is returning $quantity = '1-50' regardless of the index of the element.
I'm not sure I'm doing it correctly. Any pointers would be appreciated.
OUTPUT:
$values[] = $wpdb->prepare( "(%s, $f)", $quantity, $price );
quantityand it is not in the array. I want to set its value depending on the index of the element in the array when I run the foreach loop.