0

Here is my nested foreach loop to get the data from database

while($attrib_rec = tep_db_fetch_array($attributes)) {

foreach( $options_values as $option => $options_value ){

foreach( $options_value as $options_value_key => $each_value ){

           //make separate arrays of "option"
           print 'option = '.$option.' , value = '.$each_value.'<br />';

      }
  }
}

The output I am getting from these loops is

option = Type , value = GOLD
option = Type , value = SILVER
option = Type , value = BRONZE
option = Purity , value = Rough
option = Purity , value = Neat
option = Purity , value = Mixed
option = Purity , value = Random
option = Model , value = Old
option = Model , value = latest
option = Model , value = GOLD 1.0
option = Model , value = GOLD 1.1
option = Model , value = GOLD 1.2
option = Model , value = GOLD 1.3

what I want to achieve is to save every same "option" value an array. e.g from the above output I should get 3 arrays. i.e

$type = array('GOLD', 'SILVER', 'BRONZE');
$purity = array('Rough', 'Neat', 'mixed', 'Random');
$model = array('Old', 'Latest', 'GOLD 1.0', 'GOLD 1.1', 'GOLD 1.2', 'GOLD 1.3');

on each iteration of while loop it should make number of arrays according to "options".

If first time it makes 3 arrays(as in this example) on next iteration of while loop it might make 2 or 4 arrays depending on the same number of "Option" I get from DB.

1
  • you are using nested foreach loop which will show to the output like it is showing it... you can use filter or group by Commented Mar 2, 2016 at 11:15

3 Answers 3

2

At the moment you are just printing the value instead of storing it in an array. You probably want to change your code to something like this:

$options = array();

while($attrib_rec = tep_db_fetch_array($attributes)) {

    foreach( $options_values as $option => $options_value ){

        foreach( $options_value as $options_value_key => $each_value ){

            //Store each value by their collective key
            $options[$option][] = $each_value;
        }
    }
}

This will create an array that looks like this:

$options = array(
    'type' => array('GOLD', 'SILVER', 'BRONZE'),
    'purity' => array('Rough', 'Neat', 'mixed', 'Random'),
    'model' => array('Old', 'Latest', 'GOLD 1.0', 'GOLD 1.1', 'GOLD 1.2', 'GOLD 1.3'),
);

You can get this onto the screen like this:

echo "<pre>".print_r($options, true)."</pre>";

How this works:

Instead of printing each value we are putting them into a multidimensional array where each of your options act as the key and the values are stored in an array under the key. It's worth noting that if you have duplicate keys the values will be added to the same $option array. If this behaviour is undesirable then just use a unique identifier or if you do not need the keys to be associative you could just make the array keys increment each loop iteration.

If you want to get each value out you can do:

echo "<pre>".print_r($options['purity'], true)."</pre>";

Which would print out the values Rough, Neat, Mixes, Random.

You can find out more about multidimensional arrays here which simply explains:

A multidimensional array is an array containing one or more arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Henders for your explanation. I am very near to solving my actual problem. I wonder how would I implement the nested foreach to generate combinations (image of each value on other) if I had separate $type = array('GOLD', 'SILVER', 'BRONZE'); $purity = array('Rough', 'Neat', 'mixed', 'Random'); $model = array('Old', 'Latest', 'GOLD 1.0', 'GOLD 1.1', 'GOLD 1.2, 'GOLD 1.3');' I would use foreach( $type as $base ){ foreach( $purity as $attr1 ){ foreach( $model as $attr2 ){ //72 combinations will be generated (3 * 4 * 7) } } }
0

You may use a simple array to categorize them. Take a look at this example:

$sorted_options = array();
while($attrib_rec = tep_db_fetch_array($attributes)) {
    while($attrib_rec = tep_db_fetch_array($attributes)) {

        foreach( $options_values as $option => $options_value ){

            foreach( $options_value as $options_value_key => $each_value ){

                sorted_options[$option][] = $each_value;
            }
        }
    }
}
echo '<pre>'.print_r($sorted_options, true).'<pre>';

Comments

0

Try this -

    <?php
    $arr = array();
    while($attrib_rec = tep_db_fetch_array($attributes)) {

    foreach( $options_values as $option => $options_value ){

    foreach( $options_value as $options_value_key => $each_value ){

               //make separate arrays of "option"
               //print 'option = '.$option.' , value = '.$each_value.'<br />';
               $arr[$option][]=$each_value;
          }
      }
    }
    print_r($arr);
    ?>

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.