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:
Code:
<?php

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

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

?>
Should do it. If loading the terms from a text file you could just substitute
Code:
$terms = array("a", "b", "c", "d", "e", "f", "g");
with
Code:
$terms = file("nameoftermsfile.ext");
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 :)
 
In theory - assign each an ID, from 1 to n.

Set A=1

(A)th term and (A+1)th is your first pair
(A) and (A+2)
(A) and (A+3)
...
Up to n

Set A=(A+1)

Repeat
 
Yup that's how to do it, thanks :) Very simple alteration of the second loop lets my code do that:

Code:
<?php

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

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

?>
 
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/>";
		}
	}
}

?>
 
Set count($terms) to a variable or you're needlessly recounting it with every single iteration, which on a large array will waste precious time :)
 
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/>";
		}
	}
}

?>
 
Next few small improvements...
Code:
<?php

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

for($i = 0; $i < $count; $i++){
	for($j = $i; $j < $count; $j++){
		if ($terms[$i] != $terms[$j]) {
			$str .= $terms[$i].$terms[$j].'<br/>';
		}
	}
}

echo $str;

?>

We have an alphabet for a reason :p
 
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:
But $j has no meaning where as at least $i1 and $i2 can be interpreted as iteration 1 and iteration 2, no? :p

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

null :)
 
null said:
But $j has no meaning where as at least $i1 and $i2 can be interpreted as iteration 1 and iteration 2, no? :p
No :p

At least, I've never seen or heard it done :p

J follows on sequentially from i. Just like y follows on from x, etc :)
 
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:
null said:
But $j has no meaning where as at least $i1 and $i2 can be interpreted as iteration 1 and iteration 2, no? :p

Every style guide I've ever read and most code I've ever read uses i, j, k, l... for nested iterative loops.

Code:
for ( $i = 0; $i < $foo; $i++ ) {
    for ( $j = $i + 2; $j < $bar; $j++ ) {
        for ( $k = 2, $k <= 10; $k++ ) {
        }
    }
}

is much easier to write and read than:

Code:
for ( $i = 0; $i < $foo; $i++ ) {
    for ( $i2 = $i + 2; $i2 < $bar; $i2++ ) {
        for ( $i3 = 2, $i3 <= 10; $i3++ ) {
        }
    }
}
 
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.
 
Back
Top Bottom