Muhammad Asif Raza
Muhammad Asif Raza

Reputation: 677

PHP create separate arrays within nested loop

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.

Upvotes: 0

Views: 1198

Answers (3)

Henders
Henders

Reputation: 1215

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.

Upvotes: 2

Ganesh Gadge
Ganesh Gadge

Reputation: 410

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

Upvotes: 0

besciualex
besciualex

Reputation: 1892

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>';

Upvotes: 0

Related Questions