Jump to content


Photo

Sorting arrays


  • Please log in to reply
9 replies to this topic

#1 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 23 October 2006 - 10:31 PM

I need help with how to accomplish the following...

I have a multidimesional array with an unlimited number of rows and 5 columns called $myArray.The array is created from a database query. I would like to create 2 different displays from this array that are printed to a page. The first display would be grouping all rows which share the same value for a specified column (i.e. $myArray[$row][0]) and then printing them. Similar to:

// array data
$myArray[1][0] = "sifl";
$myArray[1][1] = "apple";
$myArray[2][0] = "olly";
$myArray[2][1] = "orange";
$myArray[3][0] = "sifl";
$myArray[3][1] = "pear";
$myArray[4][0] = "sifl";
$myArray[4][1] = "banana";

// printed output would look like

sifl
apple
pear
banana

olly
orange

The second display would be similar but it would group rows which shared the same values for specified 2 columns. If anyone has the time to help me possibly create 2 functions to accomplish this task I would appreciate it. Any information is appreciated.

Best,

Fezzik


#2 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 23 October 2006 - 11:39 PM

Can you explain what you're looking for a little more clearly?  And show any code you've tried--maybe that'll help me understand what you're after.
Don

#3 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 24 October 2006 - 12:16 AM

Sure!

So I have a multidimensional array of many rows and 5 columns per row. The data is similar to:

$myArray[0][0] = "Autobots";
$myArray[0][1] = "Bumblebee";
$myArray[0][2] = "VW Bug";
$myArray[0][3] = "Yellow";
$myArray[0][4] = "Smog Check";

$myArray[0][0] = "Autobots";
$myArray[0][1] = "Jazz";
$myArray[0][2] = "Porsche";
$myArray[0][3] = "White";
$myArray[0][4] = "Steering Realignment";

$myArray[0][0] = "Autobots";
$myArray[0][1] = "Optimus Prime";
$myArray[0][2] = "Diesel";
$myArray[0][3] = "Red and Blue";
$myArray[0][4] = "Catalytic Converter";

$myArray[0][0] = "Decepticons";
$myArray[0][1] = "Starscream";
$myArray[0][2] = "Flying Machine";
$myArray[0][3] = "Red";
$myArray[0][4] = "Girly Voice";

$myArray[0][0] = "Decepticons";
$myArray[0][1] = "Megatron";
$myArray[0][2] = "Pea Shooter";
$myArray[0][3] = "Purple and Black";
$myArray[0][4] = "Everything";

I would like to create a function that will cycle through the array and print something similar to (grouping every row by the value of the 1st column):

Autobots
-----------------
Bumblebee
Jazz
Optimus Prime

Decepticons
-----------------
Megatron
Starscream

Thus grouping and ordering the information that is printed by the value of the first column of the row. Here is my coding attempt:

// region
$previousRegion = "";
$currentRegion = "";
// company
$currentCompany = "";
$previousCompany = "";
// counter
$tradeCounter = 0;
foreach ($tradeInfo as $book) {
foreach ($book as $key => $value){
// region
if ( $key == 0 ) { 
$currentRegion = $value; 
if ( $currentRegion != $previousRegion ) { 
if ( $tradeCounter != 0 ) { echo "<br />"; }
echo "<strong>".$value."</strong><br /><br />"; 
$previousCompany = "";
}
}
// company
if ( $key == 1 ) { 
$currentCompany = $value; 
if ( $currentCompany != $previousCompany ) { echo "<span style=\"font-weight:bold;color:#5d5d5d;\">".$value."</span><br /><br />"; }
}
if ( !empty($value) && $key != 0 && $key != 1) {
echo "$value <br />\n";
}
if ( $key == 0 ) { $previousRegion = $value; }
if ( $key == 1 ) { $previousCompany = $value; }
if ( $key == 4 ) { echo "<br />"; }
$tradeCounter ++;
}
}

The variable names in my code do not reflect the sample data which I provided in this post. This code is just a reflection of my attempt to sort the array using foreach() and print the information how I would like it arranged. I hope this is clearer. Please let me know if you need additional details. Thanks for your response.

Cheers,

Fezzik

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 October 2006 - 12:33 AM

how about sorting the array or even usorting it and then looping it, dividing it by simply checking for a new value (like autobot vs. decepticon)
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 24 October 2006 - 12:54 AM

You are mixing keys with values; sorting will be nigh on impossible like this, unless you have a whitelist of keys.

Reorganise your array:

<?php

$array = array(
    'Autobots' => array(
        'Bumblebee',
        'Optimus Prime',
        /* etc */
    ),
    'Decepticons' => array(
        'Galvatron',
        'Starscream',
        /* etc */
    )
);

?>

then you can reference them as...
<?php

foreach ($array['Autobots'] as $autobot)
{
    echo $autobot . ' transform!' . chr(10);
}

foreach ($array['Decepticons'] as $decepticon)
{
    echo $decepticon . ' transform!' . chr(10);
}

?>


#6 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 24 October 2006 - 04:00 AM

Thanks for the responses. I'll see what I can come up with using your example.

#7 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 24 October 2006 - 06:36 AM

try
<?php
$myArray[0][0] = "Autobots";
$myArray[0][1] = "Bumblebee";
$myArray[0][2] = "VW Bug";
$myArray[0][3] = "Yellow";
$myArray[0][4] = "Smog Check";

$myArray[1][0] = "Autobots";
$myArray[1][1] = "Jazz";
$myArray[1][2] = "Porsche";
$myArray[1][3] = "White";
$myArray[1][4] = "Steering Realignment";

$myArray[2][0] = "Autobots";
$myArray[2][1] = "Optimus Prime";
$myArray[2][2] = "Diesel";
$myArray[2][3] = "Red and Blue";
$myArray[2][4] = "Catalytic Converter";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Starscream";
$myArray[3][2] = "Flying Machine";
$myArray[3][3] = "Red";
$myArray[3][4] = "Girly Voice";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Megatron";
$myArray[3][2] = "Pea Shooter";
$myArray[3][3] = "Purple and Black";
$myArray[3][4] = "Everything";
function gr($group_key,$data_key,$aray) {
	foreach ($aray as $a) $b[$a[$group_key]] .= " => $a[$data_key]<br />";
	foreach ($b as $c => $d) $output .= "$c<br />------------<br />$d<br />";
	return $output;	
}

print gr(0,1,$myArray);
?>


#8 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 24 October 2006 - 04:22 PM

Thanks sasa! This function is splendid! I've been trying to modify your function so that it lists all the $data_key's associated with the $group_key but have been unsuccessful. Is this easily accomplished with the function you provided?

Cheers,

Fezzik

#9 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 25 October 2006 - 08:34 PM

<?php
$myArray[0][0] = "Autobots";
$myArray[0][1] = "Bumblebee";
$myArray[0][2] = "VW Bug";
$myArray[0][3] = "Yellow";
$myArray[0][4] = "Smog Check";

$myArray[1][0] = "Autobots";
$myArray[1][1] = "Jazz";
$myArray[1][2] = "Porsche";
$myArray[1][3] = "White";
$myArray[1][4] = "Steering Realignment";

$myArray[2][0] = "Autobots";
$myArray[2][1] = "Optimus Prime";
$myArray[2][2] = "Diesel";
$myArray[2][3] = "Red and Blue";
$myArray[2][4] = "Catalytic Converter";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Starscream";
$myArray[3][2] = "Flying Machine";
$myArray[3][3] = "Red";
$myArray[3][4] = "Girly Voice";

$myArray[3][0] = "Decepticons";
$myArray[3][1] = "Megatron";
$myArray[3][2] = "Pea Shooter";
$myArray[3][3] = "Purple and Black";
$myArray[3][4] = "Everything";
function gr($group_key,$data_key,$aray) { //reorganize array
	if (!is_array($data_key)) $data_key = array($data_key);
	foreach ($aray as $a) {
		$o1 = array();
		foreach ($data_key as $e) $o1[] = $a[$e];
		$b[$a[$group_key]][] = $o1;
	}
	return $b;	
}
function out($a) { //output array
	foreach ($a as $b => $c) { 
		$out .= $b."\n".'<table border="3">'."\n";
		foreach ($c as $d) {
			$out .= '<tr>'."\n";
			foreach ($d as $e) $out .= "\t<td width=\"150\">$e</td>\n";
			$out .= '</tr>'."\n";
		}
		$out .= '</table>'."\n<br />\n\n";
	}
	return $out;
}
$a = array(1,3,4,2);
$b = gr(0,$a,$myArray);
//print_r($b);
$c = out($b);
echo $c;
?>


#10 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 25 October 2006 - 09:40 PM

Thank you again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users