Re: Merging Multi-Dimensional Arrays Into Another Array

From: Date: Thu, 21 Aug 2003 07:54:08 +0000
Subject: Re: Merging Multi-Dimensional Arrays Into Another Array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-160281@lists.php.net to get a copy of this message
* Thus wrote Nilaab Y. (superbus22@comcast.net): > Ok, here goes... > > I have an multi-dimensional array extracted from the database ($units) that > is listed at the bottom of this e-mail. What I want to do is take this > multi-dimensional array $units and maybe merge or extract it to a different > array called $options. I want to group similar units into a category. > > For example, I want to be able to group lbs. and oz. as units of weight. I > want to group cm, m, in, ft as units of width, height or depth. I will be > using this information to group these units into a drop-down menu on a form, > specific to the category (caption_name). A simple loop through the array: // the magic: define what group has what // index in the first array $option_index = array( 'weight' => 0, 'height' => 1, 'width' => 1 ... ); foreach($units as $i => $unit ) { // pull the group name out $group = $unit['caption_name']; // what index this group is assigned $index = $option_index[$group]; // create a new second level array // with the proper data. $options[$index][] = array( 'unit_id' => $unit['unit_id'], 'unit_option' => $unit['unit_option'] ); } > > $options Array > --------------- > First dimension info: > [0] = weight group > [1] = width, height, or depth group > [2] = volume group > etc... > --------------- > Array > ( > [0] => Array > ( > [0] => Array > ( > [unit_id] = 1 > [unit_option] = lbs. > ) > [1] => Array > ( > [unit_id] = 2 > [unit_option] = oz. > ) > ) > [2]... > [3]... > ) > > $units Array > --------------- > Array > ( > [0] => Array > ( > [0] => 1 > [caption_id] => 1 > [1] => weight > [caption_name] => weight > [2] => 1 > [unit_id] => 1 > [3] => lbs. > [unit_option] => lbs. > ) > ) Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#160281) next »