Page 1 of 1

Two arrays, how to 'line them up'

Posted: Tue Aug 29, 2006 10:26 pm
by robster
Hi all,

I have two arrays, one contains a list of individual postcodes: IE

Code: Select all

//get the postcodes from the database and stick it in an array.  sticking in ALL postcodes so we can count the numbers!!!
	//=======================================================================================================================
	mysql_select_db($dbname);
	$sql = "SELECT * FROM clients ORDER BY add_pcode";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$cShowMax = mysql_num_rows($content);

	//setup the array before we loop through and push data in it
	$pcode_number_array = array();

	for ($y=1; $y<=$cShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$pcode_id = $Xcontent["id"];  //get the Post Code
		$add_pcode = $Xcontent["add_pcode"];  //get the Post Code
		
		//stick them into an array
		array_push($pcode_number_array, "$add_pcode");
		
		
	$Xcontent = mysql_fetch_array($content);
	}

The other is an array with JUST the count of each postcode.

Code: Select all

//count the number of postcodes by group
$postcode_count = array_count_values($pcode_number_array);
IE: if the postcodes were:

Code: Select all

4000, 4000, 4000, 4001, 4002, 4006, 4006, 4008
Then the array $postcode_count would contain:

Code: Select all

3, 1, 1, 2, 1
What I want is a display that shows the postcode and the number of that postcode that has been counted in the database:. IE:

Code: Select all

4000   3
4001   1
4002   1
4006   2
4008   1
How can I get this data to match up?

(scratching head for too long now :))

Rob

Posted: Wed Aug 30, 2006 1:05 am
by nickvd
Assuming that both arrays are "lined up" on their own (i.e. $arr1[54] => $arr2[54] and count($arr1)==count($arr2))

Code: Select all

<?php
for ($i=0;$i<count($arr1);$i++) {
  $newArray[$i]['arr1Val'] = $arr1[$i];
  $newArray[$i]['arr2Val'] = $arr2[$i];
}
?>
Should do the trick, however it has not been tested, and may just blow up your machine ;) ...

<edit>
K, well, now that I fully read your post, that wont work :)

I would sort the array prior to calling array_count_values, that way the counts will be sorted from lowest postal code to highest (i'd assume, anyway). Then use array_unique on the postal codes, and sort it the same as the count array, then you could use the above code. i.e.

Code: Select all

<?php

$pcode = $postalCodeArray // in order to preserve the original
sort($pcode);
$count = array_count_values($pcode);
$pcode = array_unique($pcode);
// here, count($count) should == count($pcode)

for ($i=0;$i<count($pcode);$i++) {
  $newArray[$i]['code'] = $pcode[$i];
  $newArray[$i]['count'] = $count[$i];
}
?>
Again, I didnt test this, but logically it should work :)

Posted: Wed Aug 30, 2006 1:14 am
by robster
no, the problem is that the first array holds ALL of the postcodes (let's assume 300 postcodes) and the second array contains the count of each duplicate (let's assume 40 numbers).

Looking at the code above you can see where I add the individual postcodes to the first array, I am wondering if there is some step I should be taking there to get them working together.

Any advice really appreciated.


Rob

Posted: Wed Aug 30, 2006 1:18 am
by nickvd
Heh, we both clicked the post button (or edit button in my case) at the same time ;)

Posted: Wed Aug 30, 2006 2:12 am
by volka

Code: Select all

for ($y=1; $y<=$cShowMax; $y++)
{
	//Get the info from the database and smack it in variables for user later Wink
	/* why do you assign this variable? */
	$pcode_id = $Xcontent["id"];  //get the Post Code
	$add_pcode = $Xcontent["add_pcode"];  //get the Post Code

	//stick them into an array
	/* you could use $Xcontent["add_pcode"] here -> no need for add_pcode */
	/* what are the quotes arround $add_pcode good for? */
	array_push($pcode_number_array, "$add_pcode");

	/* why do you fetch the first line before the loop
		and the rest here at the end? */
	$Xcontent = mysql_fetch_array($content);
}

You can group the data by add_pcode and have the sql server count the members of each group.

Code: Select all

<?php
$dbname = 'test';
$db = mysql_connect('localhost', 'localuser', 'localpass') or die(mysql_error());

mysql_select_db($dbname, $db) or die(mysql_error());
$query = "SELECT
		add_pcode, count(add_pcode) as c
	FROM
		clients
	GROUP BY
		add_pcode
	ORDER BY
		add_pcode
	";

$result = mysql_query($query, $db) or die(mysql_error());
	
while( $row=mysql_fetch_array($result) ) {
	echo $row['add_pcode'], ' : ', $row['c'], "<br />\n";
}
?>