0

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 );
9
  • And what are the other nested arrays for? What happens if a match is found in '2A'? What is the name of the variable that has the value you want to look for? You don't seem to use it in your code... Commented Jan 29, 2018 at 19:26
  • The same if/else conditions should run. Commented Jan 29, 2018 at 19:28
  • What do you expect the out put to look lie? Commented Jan 29, 2018 at 19:28
  • The variable is quantity and 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. Commented Jan 29, 2018 at 19:29
  • @trincot of the element which it is at, when it is running the loop. Commented Jan 29, 2018 at 19:32

3 Answers 3

2

You need the index of the $value array, which you can get with the => syntax within the foreach statement. Then you can simplify the inner loop as follows:

foreach ($value as $i => $price){
    $quantity = ($i*50+1) . "-" . ($i*50+50);
    $values[] = $wpdb->prepare( "(%s, $f)", $quantity, $price );
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think this gives you what you want.

A little more data driven.

<?php

$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));

$qty = array('1-59','51-100','101-150','151-200','201-250','251-300','301-350','351-400');

foreach ( $data_array as $cp => $value ) {
    $values = array();
    for ($x=0;$x<8;$x++){
    //  echo $qty[$x] .', '. $value[$x].'<br>';
    $values[] = $wpdb->prepare( "(%s, $f)", $qty[$x], $value[$x]);
    }
}

Comments

1

You're doing wrong, You're not matching any value with if so it's always return true and assign first value which is 1-50

Note: This is suitable if your sub-array contains 8 elements, otherwise you've to add more else if corresponding to elements.

$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));

  foreach ( $data_array as $cp => $value ) {


                            foreach ($value as $k => $price){
                              if ($k == 0) {
                                 $quantity = '1-50';
                              }
                              else if ($k == 1){
                                 $quantity = '51-100';
                              }
                              else if ($k == 2){
                               $quantity = '101-150';
                              }
                              else if ($k == 3){
                                $quantity = '151-200';
                              }
                              else if ($k == 4){
                                $quantity = '201-250';
                              }
                              else if ($k == 5){
                                $quantity = '251-300';
                              }
                              else if ($k == 6){
                                $quantity = '301-350';
                              }
                              else if ($k == 7){
                                $quantity = '351-400';
                              }

                              //output
                             $values[] = $wpdb->prepare( "(%s, $f)", $quantity, $price );
                            }
}

Working example: https://3v4l.org/hZ6kq

Note: Using too much if-elseif is not good practice, use some logic instead of if-elseif.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.