Jump to content

Recommended Posts

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??

Link to comment
https://forums.phpfreaks.com/topic/182202-combinations-of-an-array/
Share on other sites

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.

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";

 

}

}

}

}

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..

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?

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, '&#8801;', 'B', '&#8801;'),

(16, '>', 'C', '<'),

(17, '<', 'O', '>'),

(18, '&#926;', 'B', '&#926;'),

(20, '&#9760;', 'B', '&#9760;');

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.

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..

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.

Ah, that makes more sense now :facepalm: 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 :)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.