Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Finding Array Combinations
Old 07-18-2011, 08:58 PM Finding Array Combinations
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I need help! I am trying to find a way to get all the possible combinations of all attribute-value pairs in an array. For example, I have two arrays $attributes where the index is the attribute id and the value is the attribute name. The other array is $values where the index is the attribute id and the value is an array of all the related value names.

For example: (shortened for example)

PHP Code:
$attributes = array(
    
10 => 'school_type',
    
20 => 'grade_level',
    
30 => 'format',
);
 
$values = array(
    
// For attribute school_type
    
10 => array(
        
'Public School',
        
'Home School',
    ),
    
    
// For attribute grade_level
    
20 => array(
        
'1st Grade',
        
'2nd Grade',
        
'3rd Grade',
    ),
    
    
// For attribute format
    
30 => array(
        
'Individual',
        
'Group',
    ),
); 
I need a way to create a master array with all possible combinations for each attribute-value pair so the resulting array would look like this:

Code:
Array
(
    [0] => Array
        (
            [10] => Public School
            [20] => 1st Grade
            [30] => Individual
        )
 
    [1] => Array
        (
            [10] => Public School
            [20] => 1st Grade
            [30] => Group
        )
 
    [2] => Array
        (
            [10] => Public School
            [20] => 2nd Grade
            [30] => Individual
        )
 
    [3] => Array
        (
            [10] => Public School
            [20] => 2nd Grade
            [30] => Group
        )
 
    [4] => Array
        (
            [10] => Public School
            [20] => 3rd Grade
            [30] => Individual
        )
 
    [5] => Array
        (
            [10] => Public School
            [20] => 3rd Grade
            [30] => Group
        )
 
    [6] => Array
        (
            [10] => Home School
            [20] => 1st Grade
            [30] => Individual
        )
 
    [7] => Array
        (
            [10] => Home School
            [20] => 1st Grade
            [30] => Group
        )
 
    [8] => Array
        (
            [10] => Home School
            [20] => 2nd Grade
            [30] => Individual
        )
 
    [9] => Array
        (
            [10] => Home School
            [20] => 2nd Grade
            [30] => Group
        )
 
    [10] => Array
        (
            [10] => Home School
            [20] => 3rd Grade
            [30] => Individual
        )
 
    [11] => Array
        (
            [10] => Home School
            [20] => 3rd Grade
            [30] => Group
        )
 
)
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-19-2011, 03:57 AM Re: Finding Array Combinations
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Perhaps you've though if this already, but I think what you're looking for is the cartesian product of the values array, possibly with some modifictation to get your indices correct.

I found a script that seems to be what you're looking for. http://ideone.com/7mRCL
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 07-19-2011, 07:00 PM Re: Finding Array Combinations
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Sweetness! - I can get this to work, thank you.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Finding Array Combinations
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.65458 seconds with 12 queries