Forum Moderators: coopster

Message Too Old, No Replies

Grouping results in an array

         

fintan

2:56 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



I have a query that runs against a database and returns a result in an array.

The array contain names of people and what section they belong to. Each section has a unique id. So I want to group each person by the section id. Any pointers? Thanks

fintan.

coopster

3:22 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You mean like asort() [php.net]?

fintan

3:43 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Kinda but lets say

$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple", "c" => "peach", "b" => "pear",);

What I want is something like this.

d = lemon
a = orange
b = banana, pear
c = apple, peach

The arrays a bit more complecated than that though. Here's some of it


Array ( [0] => Array ( [Section] => District Office [0] => District Office [SectionID] => 2 [1] => 2 [StaffNumber] => 0555339 [2] => 0555339 [Forename] => somename [3] => J [Surname] => somename [4] => somename ) [1] => Array ( [Section] => District Office [0] => District Office [SectionID] => 4 [1] => 4 [StaffNumber] => 0570362 [2] => 0570362 [Forename] => somename [3] => somename [Surname] => somename [4] => somename )

coopersita

4:16 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Maybe this thread will help:

[webmasterworld.com...]

fintan

9:38 am on Jun 17, 2005 (gmt 0)

10+ Year Member



I'm using access as the database backend and the group by sql function is slightly different. If I do an simple order by. That would put the result in proper order. What I need to get my head around is searching the array and associating everything with the section id. If I do two queries would I be able to loop through two of them.

select section, sectionid from table
select forname, surname, secitonid from table2

Then do something like this

if (!(strcmp($rslt['sectionid'], $rslt2["sectionid"]))){
do something
}

Would that work?

supermanjnk

1:27 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



you could try a multi dimension array like this:

$array = array();
$query = (query statement)
while ($res = mysql_fetch_object($query)) {
$array["$res->letter"][$res->fruit] = "";
}
which would make each letter and actuall array
array would contain:

so you would have
$array[0] => a
$array[1] => b
$array[2] => c
$array[3] => d

so now if you do this
$fruit = $array[1]

each key in fruit is now one of the fruits for the key that corisponds to $array[1]

so then you would have:
$fruit[banana] => ""
$fruit[pear] => ""

fintan

2:31 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



So what would happen to the duplicates then? Since the key is replaced with banana. What happens when banana has more than one set of values.

banana could be green or yellow. If the first value is green then the next one is yellow would banana become yellow or would it be green and yellow. Am I making sense?

coopster

2:35 pm on Jun 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Perhaps array_merge_recursive() [php.net] will work for you in this case.

fintan

4:03 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



Thanks for the tips I think I know which way I'm going to go with this

coopster

4:36 pm on Jun 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Do share when you get it ;)

It's an interesting discussion and I'm certain folks that are following will be interested to see how you resolve the situation.

fintan

3:30 pm on Jul 22, 2005 (gmt 0)

10+ Year Member



Ok I know it's a while sine I last posted. Been up to my eye's getting crap out of the way. Anyway finally took a look at this again. Here's what I got


function build(){
$x = 0;
$rsSurname = "SELECT Section.SectionID, Section.Section, WorksPhone.StaffNumber, Forename, Surname
FROM Section INNER JOIN WorksPhone ON Section.[Section Code] = WorksPhone.[Section Code] WHERE Employee=-1
ORDER BY SectionID ASC";
$surname = new Get_Sql(); $surname_rslts = $surname->SqlLoop($rsSurname);
$surname_count = $surname->SqlCount($rsSurname);

while ($x < $surname_count){
$a[] = $surname_rslts[$x++]['SectionID']; // Builds the array as $a
}

$b = array_unique($a);// Distinct number of Section id's
$b_count = count($b); // Counts values in array
$c = array_values($b); // Gets the values but also more importantly resets the array keys

function findkey($source, $searchkey){

$firsttimeround = true;
$secondtimeround = false;

reset($source);
while (list($key, $val) = each($source)) {
if (!(strcmp($searchkey, $val))){
// echo $key.'<br>';
$newindex[] = $key;
// print_r($newindex);
}
}

return $newindex;

}
for($i = 0; $i < $b_count ; $i++){
$find = findkey($a, $c[$i]); // test 79 for 79 there are 11 occurances
print_r($find); echo '<br>';
// return $find;
}

}

print_r(build());

It still needs refindment. I have to do some more tweeks. Like increasing $newindex key by 1 so it's not 0 and searching the original array "$surname_rslts" for the groupped results.

fintan

11:58 am on Jul 25, 2005 (gmt 0)

10+ Year Member



This is the finished result. If anyone can see a better way please tell me. Thanks


function build(){
$x = 0;
$rsSurname = "SELECT Section.SectionID, Section.Section, WorksPhone.StaffNumber, Forename, Surname
FROM Section INNER JOIN WorksPhone ON Section.[Section Code] = WorksPhone.[Section Code] WHERE Employee=-1
ORDER BY SectionID ASC";
$surname = new Get_Sql(); $surname_rslts = $surname->SqlLoop($rsSurname);
$surname_count = $surname->SqlCount($rsSurname);

while ($x < $surname_count){
$a[] = $surname_rslts[$x++]['SectionID']; // Builds the array as $a
}

$b = array_unique($a);// Distinct number of Section id's
$b_count = count($b); // Counts values in array
$c = array_values($b); // Gets the values but also more importantly resets the array keys

function findkey($source, $searchkey){

reset($source);
while (list($key, $val) = each($source)) {

if (!(strcmp($searchkey, $val))){
$newindex[] = $key;
}
}

return $newindex;

}
for($i = 0; $i < $b_count ; $i++){ // Gathers all the keys from the original array
$find = findkey($a, $c[$i]); // Begins the search for the array keys
$find_count = count($find); // Counts how many values are in the array

for($z = 0; $z <= $find_count ; $z++){ // Finnally the finished result
print_r($surname_rslts[$find[$z]]);echo '<br>';
}
}
}

print_r(build());