Php sorting?

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
Say I have:

Code:
a
b
c
d
e
f
g

How would I generate crosses of each of these?

i.e.

Code:
a b
a c
a d
a e
a f
a g
b c
b d
b e
b f
b g
...etc....

Thinking about it, a-b would be the same as b-a so only one of these would be needed.

It *has* to be simple?

(actually data set has >800 'terms' instead of a->g :))

edit - 'terms' are stored flatfile and also in a mysql column if that makes anythign easier.
 
Last edited:
null said:
As for making sure a-b and not b-a is output, I would have to think about that one.

Hope this is what you meant and it helps, null :)
Ooooh works!

One big prob, it does "a a" - which can't be allowed. So either, can we change the script to 'not' do a a, or remove a a b b c c etc.. post script?

edit, done:

Code:
<?php
//$terms = file("Acc.txt");
$terms = array("a", "b", "c", "d", "e", "f", "g");

for($i1 = 0; $i1 < count($terms); $i1++){
	for($i2 = 0; $i2 < count($terms); $i2++){
		if ($terms[$i1]==$terms[$i2]) {
		}else{
		echo $terms[$i1].$terms[$i2]."<br/>";
		}
	}
}
?>
 
Last edited:
Cheers!

Combined yours with my if bit:

Code:
<?php

$terms = array("a", "b", "c", "d", "e", "f", "g");

for($i1 = 0; $i1 < count($terms); $i1++){
	for($i2 = $i1; $i2 < count($terms); $i2++){
		if ($terms[$i1]==$terms[$i2]) {
		}else{
		echo $terms[$i1].$terms[$i2]."<br/>";
		}
	}
}

?>
 
null said:
While we're at it, I think the if statement looks better like this :p:

Code:
<?php

$terms = array("a", "b", "c", "d", "e", "f", "g");
$count = count($terms);

for($i1 = 0; $i1 < $count; $i1++){
	for($i2 = $i1; $i2 < $count; $i2++){
		if ($terms[$i1] != $terms[$i2]) {
			echo $terms[$i1].$terms[$i2]."<br/>";
		}
	}
}

?>
Indeed it does and duly noted! However, I am currently running 658,532 subsequent mysql queries.... so... aint gonna change it just yet! (dual core goodness)...

Why I'm at it, no point starting a new thread.

Basically I've taken $terms[$i1] and $terms[$i2] and looked them up in my db to yield the results $go1 and $g02 respectively.

Now, wjat I'd like to be able to is compare $go1 to $go2 and return as $goresults ONLY those 'terms' present in BOTH fields . ($go1/2 contains tab seperated numbers, e.g. 1tab2tab3tab etc....).

I THEN need to use this data to do a second mysql db call - but I'll sort that later... right now I want to figure out how to calcualte $goresults as only those terms shared in $g01 and 2. Ideas team? :D

(why do I give myself these stupid projects!)

Beansprout said:
Next few small improvements...
We have an alphabet for a reason :p
???
 
Last edited:
null said:
jdickerson I'd try to help but I don't think I understand what you want to do, and I'd be going out of my depth probably because I don't know anything about databases. :o

Basically, what I mean is.. assuming the above mysql 'fetch' works... we'll have four pieces of info for EACH line (329,669 lines):
1st term (i)
->"GO" from db for i

2nd term (j)
->"GO" from db for j

I want to check the value of I (tab seperated numbers - e.g. 1 2 3 4 5) against J. I want to then output any DIFFERENCES found.

I.e. I = 1 2 3 4 5, J = 4 5 6 7 8. OUTPUT would be = 4 5.

I could do all this in steps, i.e. use the script to take acc.txt and write acc2.txt contain a b, a c, a d so on... then read this to generate the GO values and so on... but I'd rather do it all in one go...
 
Last edited:
Dj_Jestar said:
On a side note re: reiterative use of count() - it actually references the hashes integer that is only updated when you create, add an item, or remove an item from the array (the engine stores arrays as a hash in memory)

So the difference is a lot smaller than you think, because count() doesn't 'count'

iirc it actually has 2 function calls, one of those being a function fork.
Will this affect functionality?

Also, has anyone any idea for post #17? :(
 
Hector said:
i'm sure i read in the past that some languages have had issues with numbers in var names, perhaps more so where a var's data is a number. i think to be extra safe a lot of folk got into a habit of avoiding using numbers in vars name full stop.

so i don't think people are just being fussy with this i1/i2 stuff, use iA, iB if doing such makes it easier for you to read.. not everyone uses i, j because j is just an i with a tail. i've seen a lot of folk over the years skip j and use i, k much like the army skip five during countdowns.
My coding is so bad I often do use numbers.. to be honest, I didn't really know it was bad. I just understood it more with numbers than letters... e.g. a1 and a2 would mean more to me than foo and bar...
 
Back
Top Bottom