...

View Full Version : Combine Multiple Arrays



pinkotoad
03-15-2007, 01:54 AM
I am having problems coming up with the logic for combining multiple arrays.


<?php
/* this is the data im given, the number of sets will not always be 2,
could be more, or could just be 1 */
$data['title'] = 'Widget';
$data['price'] = 5;

$optionSets = array(
1 => array('id'=>1,'title'=>'Color'),
2 => array('id'=>2,'title'=>'Size'),
3 => array('id'=>3,'title'=>'Foofoo'),
);

$data['options'][1] = array(
array('id'=>1,'title'=>'Red','price'=>0),
array('id'=>2,'title'=>'Blue','price'=>.75),
array('id'=>3,'title'=>'White','price'=>.5),
);

$data['options'][2] = array(
array('id'=>4,'title'=>'Small','price'=>0),
array('id'=>5,'title'=>'Large','price'=>.5),
array('id'=>8,'title'=>'XLarge','price'=>1.5),
);

// this the array i need to end up with
$new_set = array(
array ('title' => 'Red Small', 'inc'=>0, 'id'=>array('4','1') ),
array ('title' => 'Red Large', 'inc'=>.5, 'id'=>array('5','1') ),

array ('title' => 'Blue Small', 'inc'=>2.5, 'id'=>array('5','2') ),
array ('title' => 'Blue Large', 'inc'=>3, 'id'=>array('5','2') ),

array ('title' => 'White Small', 'inc'=>3, 'id'=>array('5','3') ),
array ('title' => 'White Large', 'inc'=>3.5, 'id'=>array('5','3') ),
);
?>

Anyone have any ideas?

pinkotoad
03-15-2007, 02:35 AM
Its finally working:

<pre><?php

function combineArrays($arrays) {
$numArrays = count($arrays);
for($i=0;$i<$numArrays;$i++) {
$vars = count($arrays[$i]);
if($i == 0) $combos = $vars;
else $combos = $combos * $vars;
$statInfo[$i]['vars'] = $vars;
$statInfo[$i]['combos'] = $combos;
}

$key = 0;
$rotationCounter = 1;

for($set=0;$set<$numArrays;$set++) {
$vars = $statInfo[$set]['vars'];
$rotation = $statInfo[$set]['combos'];
$displayed = $combos / $rotation;

$displayCounter = 1;
for($j=0; $j<$combos; $j++) {
$value = $arrays[$set][$key];
$results[$j][] = $value;

if($displayCounter == $displayed) {
$key++;
$displayCounter = 0;
if($key >= $vars) $key = 0;
}
$displayCounter++;
}
$key = 0;
}

return $results;
}

$optionSets = array(
1 => array('id'=>1,'title'=>'Color'),
2 => array('id'=>2,'title'=>'Size'),
3 => array('id'=>3,'title'=>'Foofoo'),
);

$data['options'][1] = array(
array('id'=>1,'title'=>'Red','price'=>0),
array('id'=>2,'title'=>'Blue','price'=>.75),
array('id'=>3,'title'=>'White','price'=>.5),
);

$data['options'][2] = array(
array('id'=>4,'title'=>'Small','price'=>0),
array('id'=>5,'title'=>'Large','price'=>.5),
array('id'=>8,'title'=>'XLarge','price'=>1.5),
);

$i = 0;
foreach($optionSets AS $set) {
if (is_array($data['options'][$set['id']])) {
foreach ($data['options'][$set['id']] AS $key => $option) {
$options[$i][] = "{$option['id']}";
}
$i++;
}
}

$resultsArray = combineArrays($options);
echo "Results: <br />";
var_dump($resultsArray);

?>


Results:
array(9) {
[0]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "4"
}
[1]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "5"
}
[2]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "8"
}
[3]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "4"
}
[4]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "5"
}
[5]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "8"
}
[6]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "4"
}
[7]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "5"
}
[8]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "8"
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum