evildobbi Posted November 19, 2009 Share Posted November 19, 2009 So I have a mysql database which I pull all the info from.. Which I then make into an array for referance.. Now is their a more efficent way to get all combinations of the array? Instead of doing: foreach ($array as $needles) { foreach ($array as $needles2) { foreach ($array as $needles3) { foreach ($array as $needles4) { $x++; echo "$x - $needles - $needles2 - $needles3 - $needles4<BR>\n"; } } } } Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/ Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 hmm, what is the structure of your end array? it seems to be a 4d array Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961391 Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 Show us your MySQL statement, and I am sure we can help you design a better array Namely what will happen will be you create a multi-dimensional array, so you would use say the ID of the table row as the key then inside that key would have the attributes: $rArray = array(1 => array("name" => "Jack", "phone" => "555-555-5555"), 2 => array("name" => "Harold", "phone" => "666-666-6666")); Is a rough example of how the array would be setup then to access all information later you would do: foreach ($rArray as $item) { echo $item["name"] . " has a phone number of " . $item["phone"] . ".<br />"; } Hopefully that helps you get an idea of where this is going. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961393 Share on other sites More sharing options...
evildobbi Posted November 19, 2009 Author Share Posted November 19, 2009 DB Pulled from: CREATE TABLE IF NOT EXISTS `rc_tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tag` varchar(25) NOT NULL, `type` varchar(1) NOT NULL, `opposite` varchar(25) NOT NULL COMMENT 'oppsite of open tag', PRIMARY KEY (`id`), UNIQUE KEY `type` (`tag`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ; So far it has 20 entries.. So it then gets put into an array and then the rest is history.. 4 is the max combinations I want and then scale it down to 2... But array comes out like a normal one $array = array('key','key','key'); Php call: Open_Mysql(); $_SQL = "SELECT type,tag as split from rc_tags"; $result = $Connect->query("$_SQL"); if(!$Connect->query("$_SQL")) { echo $Error->ReportMsg("1",$Error->ReportError("1","","1","Tags","1")); } while ($data = mysqli_fetch_array($result)) { array_push($tags, $data['split']); } $result->close(); Close_Mysql(); foreach ($tags as $needles) { foreach ($tags as $needles2) { foreach ($tags as $needles3) { foreach ($tags as $needles4) { $x++; echo "$x - $needles - $needles2 - $needles3 - $needles4<BR>\n"; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961396 Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 Can you post the code where you retrieve the data from the database and put them into arrays? That will help me grab my mind around what you are doing to help make this more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961399 Share on other sites More sharing options...
evildobbi Posted November 19, 2009 Author Share Posted November 19, 2009 DB Pulled from: CREATE TABLE IF NOT EXISTS `rc_tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tag` varchar(25) NOT NULL, `type` varchar(1) NOT NULL, `opposite` varchar(25) NOT NULL COMMENT 'oppsite of open tag', PRIMARY KEY (`id`), UNIQUE KEY `type` (`tag`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ; So far it has 20 entries.. So it then gets put into an array and then the rest is history.. 4 is the max combinations I want and then scale it down to 2... But array comes out like a normal one $array = array('key','key','key'); Php call: Open_Mysql(); $_SQL = "SELECT type,tag as split from rc_tags"; $result = $Connect->query("$_SQL"); if(!$Connect->query("$_SQL")) { echo $Error->ReportMsg("1",$Error->ReportError("1","","1","Tags","1")); } while ($data = mysqli_fetch_array($result)) { array_push($tags, $data['split']); } $result->close(); Close_Mysql(); foreach ($tags as $needles) { foreach ($tags as $needles2) { foreach ($tags as $needles3) { foreach ($tags as $needles4) { $x++; echo "$x - $needles - $needles2 - $needles3 - $needles4<BR>\n"; } } } } UP above.. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961402 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 if tags is a simple array, with a format like ('entry1', 'entry2', 'entry3') then you only need 1 foreach loop. I don't really understand why you are nesting foreach loops with the same array. Exactly what are you trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961405 Share on other sites More sharing options...
evildobbi Posted November 19, 2009 Author Share Posted November 19, 2009 if tags is a simple array, with a format like ('entry1', 'entry2', 'entry3') then you only need 1 foreach loop. I don't really understand why you are nesting foreach loops with the same array. Exactly what are you trying to accomplish? With one array it does'nt get the combinations.. It just gets 4 of the same tag... I need all tags to be represented... DB: INSERT INTO `rc_tags` (`id`, `tag`, `type`, `opposite`) VALUES (1, '/', 'B', '\\'), (2, '\\', 'B', '/'), (3, '{', 'O', '}'), (4, '}', 'C', '{'), (5, '|', 'B', '|'), (6, '[', 'O', ']'), (7, ']', 'C', '['), (8, '(', 'O', ')'), (9, ')', 'C', '('), (10, '-', 'B', '-'), (11, '_', 'B', '_'), (12, '=', 'B', '='), (15, '%', 'B', '%'), (14, '≡', 'B', '≡'), (16, '>', 'C', '<'), (17, '<', 'O', '>'), (18, 'Ξ', 'B', 'Ξ'), (20, '☠', 'B', '☠'); Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961408 Share on other sites More sharing options...
Ken2k7 Posted November 19, 2009 Share Posted November 19, 2009 Hello evildobbi, Can you post an example of the value of $array and show me what you want the output to be? Basically just post an example of what you want to accomplish. Thanks, Ken Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961410 Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 I think this is what you were getting at, I could be wrong but yea: <?php $_SQL = "SELECT type,tag as split from rc_tags Order By type"; $result = $Connect->query("$_SQL"); if(!$Connect->query("$_SQL")) { echo $Error->ReportMsg("1",$Error->ReportError("1","","1","Tags","1")); } $tags = array(); while ($data = mysqli_fetch_array($result)) { //array_push($tags, $data['split']); $tags[$data['type']][] = $data['split']; } $result->close(); Close_Mysql(); $x = 0; foreach ($tags as $type => $needles) { if ($type != $old_type) { $old_type = $type; $x++; echo "\n<br /> $x"; }else { echo " - $needles"; } } ?> Un-tested, but hopefully it simplifies things for you. EDIT: Added an order by to the SQL clause for the array organization. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961414 Share on other sites More sharing options...
evildobbi Posted November 19, 2009 Author Share Posted November 19, 2009 Hello evildobbi, Can you post an example of the value of $array and show me what you want the output to be? Basically just post an example of what you want to accomplish. Thanks, Ken with one loop i would get "/ - / - / - /" ... What I want is.... / - = - [ - ☠ each tag to be different ... It needs to represent each tag.. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961418 Share on other sites More sharing options...
evildobbi Posted November 19, 2009 Author Share Posted November 19, 2009 Type is irrelevent for this call. I was going to have it only do Open or closer but I need it to use all tags for what it needs to do... All combinations is what i need Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961424 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 so you just want a string with each different tag? you could simply do $string = ""; foreach($tags as $tag){ $string .= " ".$tag; } echo $string; if thats not what you want than I really have no clue what you are trying to do Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961426 Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 Ok, so now I am a little confused, does this not work? <?php $_SQL = "SELECT DISTINCT tag as split from rc_tags"; $result = $Connect->query("$_SQL"); if(!$Connect->query("$_SQL")) { echo $Error->ReportMsg("1",$Error->ReportError("1","","1","Tags","1")); } $tags = array(); while ($data = mysqli_fetch_array($result)) { //array_push($tags, $data['split']); $tags[] = $data['split']; } $result->close(); Close_Mysql(); foreach ($tags as $needles) { echo $needles . " - "; } ?> If you want them seperated onto seperate lines after x call that can be done using Modulus, will provide an example if that is what you are after. EDIT: mike beat me to it, but mine shows the mysql portion to build the array, so going ahead and posting. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961431 Share on other sites More sharing options...
evildobbi Posted November 19, 2009 Author Share Posted November 19, 2009 http://99.247.185.60/lookingfor.txt with the 4 foreach loops which is a resource hog... This is what I want.. Every combination of each tag.. :| Any better methold Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961441 Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 Ah, that makes more sense now lol! I think it can be handeled with recursion, but I am sure it is just as much of a hog as the 4 - foreach loops would be. I have never really been good with that stuff, so yea. I will leave it to be someone with more brain power to me to either provide a better example or say that is the best way to do it Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961444 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 I'd have to agree. Getting all the different possible combination possible for each character is a daunting task to begin with. Recursion would probably not be any better, but it may be more readable, and easier to understand. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961447 Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 Something I was just thinking of, how often are you going to add a tag to the database? The reason I ask is that you could create another table with all the combinations in it, that way you have them all in a convient place to pull the data out easily and you only have to run this script once, or each time you add a new tag and update the database with the new combinations.... Just an idea. Quote Link to comment https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/#findComment-961452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.