Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Detroit MI
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Combine Multiple Arrays

    I am having problems coming up with the logic for combining multiple arrays.

    PHP Code:
    <?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(
            
    =>    array('id'=>1,'title'=>'Color'),
            
    =>    array('id'=>2,'title'=>'Size'),
            
    =>    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?
    Last edited by pinkotoad; 03-15-2007 at 03:36 AM.
    weirdness

  • #2
    New Coder
    Join Date
    Jul 2002
    Location
    Detroit MI
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Its finally working:
    PHP Code:
    <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(
            
    =>    array('id'=>1,'title'=>'Color'),
            
    =>    array('id'=>2,'title'=>'Size'),
            
    =>    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);
        
    ?>
    Code:
    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"
      }
    }
    weirdness


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •